<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>2. Built-in Functions &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="The Python Standard Library" href="index.html" />
    <link rel="next" title="3. Built-in Constants" href="constants.html" />
    <link rel="prev" title="1. Introduction" href="intro.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="constants.html" title="3. Built-in Constants"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="1. Introduction"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" accesskey="U">The Python Standard Library</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="built-in-functions">
<span id="built-in-funcs"></span><h1>2. Built-in Functions<a class="headerlink" href="#built-in-functions" title="Permalink to this headline">¶</a></h1>
<p>The Python interpreter has a number of functions and types built into it that
are always available.  They are listed here in alphabetical order.</p>
<table border="1" class="docutils">
<colgroup>
<col width="21%" />
<col width="19%" />
<col width="20%" />
<col width="18%" />
<col width="22%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head"></th>
<th class="head"></th>
<th class="head">Built-in Functions</th>
<th class="head"></th>
<th class="head"></th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><a class="reference internal" href="#abs" title="abs"><tt class="xref py py-func docutils literal"><span class="pre">abs()</span></tt></a></td>
<td><a class="reference internal" href="#func-dict"><tt class="docutils literal"><span class="pre">dict()</span></tt></a></td>
<td><a class="reference internal" href="#help" title="help"><tt class="xref py py-func docutils literal"><span class="pre">help()</span></tt></a></td>
<td><a class="reference internal" href="#min" title="min"><tt class="xref py py-func docutils literal"><span class="pre">min()</span></tt></a></td>
<td><a class="reference internal" href="#setattr" title="setattr"><tt class="xref py py-func docutils literal"><span class="pre">setattr()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#all" title="all"><tt class="xref py py-func docutils literal"><span class="pre">all()</span></tt></a></td>
<td><a class="reference internal" href="#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a></td>
<td><a class="reference internal" href="#hex" title="hex"><tt class="xref py py-func docutils literal"><span class="pre">hex()</span></tt></a></td>
<td><a class="reference internal" href="#next" title="next"><tt class="xref py py-func docutils literal"><span class="pre">next()</span></tt></a></td>
<td><a class="reference internal" href="#slice" title="slice"><tt class="xref py py-func docutils literal"><span class="pre">slice()</span></tt></a></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#any" title="any"><tt class="xref py py-func docutils literal"><span class="pre">any()</span></tt></a></td>
<td><a class="reference internal" href="#divmod" title="divmod"><tt class="xref py py-func docutils literal"><span class="pre">divmod()</span></tt></a></td>
<td><a class="reference internal" href="#id" title="id"><tt class="xref py py-func docutils literal"><span class="pre">id()</span></tt></a></td>
<td><a class="reference internal" href="#object" title="object"><tt class="xref py py-func docutils literal"><span class="pre">object()</span></tt></a></td>
<td><a class="reference internal" href="#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#ascii" title="ascii"><tt class="xref py py-func docutils literal"><span class="pre">ascii()</span></tt></a></td>
<td><a class="reference internal" href="#enumerate" title="enumerate"><tt class="xref py py-func docutils literal"><span class="pre">enumerate()</span></tt></a></td>
<td><a class="reference internal" href="#input" title="input"><tt class="xref py py-func docutils literal"><span class="pre">input()</span></tt></a></td>
<td><a class="reference internal" href="#oct" title="oct"><tt class="xref py py-func docutils literal"><span class="pre">oct()</span></tt></a></td>
<td><a class="reference internal" href="#staticmethod" title="staticmethod"><tt class="xref py py-func docutils literal"><span class="pre">staticmethod()</span></tt></a></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#bin" title="bin"><tt class="xref py py-func docutils literal"><span class="pre">bin()</span></tt></a></td>
<td><a class="reference internal" href="#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a></td>
<td><a class="reference internal" href="#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a></td>
<td><a class="reference internal" href="#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a></td>
<td><a class="reference internal" href="#func-str"><tt class="docutils literal"><span class="pre">str()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#bool" title="bool"><tt class="xref py py-func docutils literal"><span class="pre">bool()</span></tt></a></td>
<td><a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a></td>
<td><a class="reference internal" href="#isinstance" title="isinstance"><tt class="xref py py-func docutils literal"><span class="pre">isinstance()</span></tt></a></td>
<td><a class="reference internal" href="#ord" title="ord"><tt class="xref py py-func docutils literal"><span class="pre">ord()</span></tt></a></td>
<td><a class="reference internal" href="#sum" title="sum"><tt class="xref py py-func docutils literal"><span class="pre">sum()</span></tt></a></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#bytearray" title="bytearray"><tt class="xref py py-func docutils literal"><span class="pre">bytearray()</span></tt></a></td>
<td><a class="reference internal" href="#filter" title="filter"><tt class="xref py py-func docutils literal"><span class="pre">filter()</span></tt></a></td>
<td><a class="reference internal" href="#issubclass" title="issubclass"><tt class="xref py py-func docutils literal"><span class="pre">issubclass()</span></tt></a></td>
<td><a class="reference internal" href="#pow" title="pow"><tt class="xref py py-func docutils literal"><span class="pre">pow()</span></tt></a></td>
<td><a class="reference internal" href="#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#bytes" title="bytes"><tt class="xref py py-func docutils literal"><span class="pre">bytes()</span></tt></a></td>
<td><a class="reference internal" href="#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a></td>
<td><a class="reference internal" href="#iter" title="iter"><tt class="xref py py-func docutils literal"><span class="pre">iter()</span></tt></a></td>
<td><a class="reference internal" href="#print" title="print"><tt class="xref py py-func docutils literal"><span class="pre">print()</span></tt></a></td>
<td><a class="reference internal" href="#func-tuple"><tt class="docutils literal"><span class="pre">tuple()</span></tt></a></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#callable" title="callable"><tt class="xref py py-func docutils literal"><span class="pre">callable()</span></tt></a></td>
<td><a class="reference internal" href="#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a></td>
<td><a class="reference internal" href="#len" title="len"><tt class="xref py py-func docutils literal"><span class="pre">len()</span></tt></a></td>
<td><a class="reference internal" href="#property" title="property"><tt class="xref py py-func docutils literal"><span class="pre">property()</span></tt></a></td>
<td><a class="reference internal" href="#type" title="type"><tt class="xref py py-func docutils literal"><span class="pre">type()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#chr" title="chr"><tt class="xref py py-func docutils literal"><span class="pre">chr()</span></tt></a></td>
<td><a class="reference internal" href="#func-frozenset"><tt class="docutils literal"><span class="pre">frozenset()</span></tt></a></td>
<td><a class="reference internal" href="#func-list"><tt class="docutils literal"><span class="pre">list()</span></tt></a></td>
<td><a class="reference internal" href="#func-range"><tt class="docutils literal"><span class="pre">range()</span></tt></a></td>
<td><a class="reference internal" href="#vars" title="vars"><tt class="xref py py-func docutils literal"><span class="pre">vars()</span></tt></a></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#classmethod" title="classmethod"><tt class="xref py py-func docutils literal"><span class="pre">classmethod()</span></tt></a></td>
<td><a class="reference internal" href="#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a></td>
<td><a class="reference internal" href="#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a></td>
<td><a class="reference internal" href="#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a></td>
<td><a class="reference internal" href="#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a></td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a></td>
<td><a class="reference internal" href="#globals" title="globals"><tt class="xref py py-func docutils literal"><span class="pre">globals()</span></tt></a></td>
<td><a class="reference internal" href="#map" title="map"><tt class="xref py py-func docutils literal"><span class="pre">map()</span></tt></a></td>
<td><a class="reference internal" href="#reversed" title="reversed"><tt class="xref py py-func docutils literal"><span class="pre">reversed()</span></tt></a></td>
<td><a class="reference internal" href="#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a></td>
</tr>
<tr class="row-even"><td><a class="reference internal" href="#complex" title="complex"><tt class="xref py py-func docutils literal"><span class="pre">complex()</span></tt></a></td>
<td><a class="reference internal" href="#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a></td>
<td><a class="reference internal" href="#max" title="max"><tt class="xref py py-func docutils literal"><span class="pre">max()</span></tt></a></td>
<td><a class="reference internal" href="#round" title="round"><tt class="xref py py-func docutils literal"><span class="pre">round()</span></tt></a></td>
<td>&nbsp;</td>
</tr>
<tr class="row-odd"><td><a class="reference internal" href="#delattr" title="delattr"><tt class="xref py py-func docutils literal"><span class="pre">delattr()</span></tt></a></td>
<td><a class="reference internal" href="#hash" title="hash"><tt class="xref py py-func docutils literal"><span class="pre">hash()</span></tt></a></td>
<td><a class="reference internal" href="#func-memoryview"><tt class="docutils literal"><span class="pre">memoryview()</span></tt></a></td>
<td><a class="reference internal" href="#func-set"><tt class="docutils literal"><span class="pre">set()</span></tt></a></td>
<td>&nbsp;</td>
</tr>
</tbody>
</table>
<dl class="function">
<dt id="abs">
<tt class="descname">abs</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#abs" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the absolute value of a number.  The argument may be an
integer or a floating point number.  If the argument is a complex number, its
magnitude is returned.</p>
</dd></dl>

<dl class="function">
<dt id="all">
<tt class="descname">all</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#all" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if all elements of the <em>iterable</em> are true (or if the iterable
is empty).  Equivalent to:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">all</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">element</span><span class="p">:</span>
            <span class="k">return</span> <span class="k">False</span>
    <span class="k">return</span> <span class="k">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="any">
<tt class="descname">any</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#any" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <tt class="docutils literal"><span class="pre">True</span></tt> if any element of the <em>iterable</em> is true.  If the iterable
is empty, return <tt class="docutils literal"><span class="pre">False</span></tt>.  Equivalent to:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">any</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span>
    <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">element</span><span class="p">:</span>
            <span class="k">return</span> <span class="k">True</span>
    <span class="k">return</span> <span class="k">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="ascii">
<tt class="descname">ascii</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#ascii" title="Permalink to this definition">¶</a></dt>
<dd><p>As <a class="reference internal" href="#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a>, return a string containing a printable representation of an
object, but escape the non-ASCII characters in the string returned by
<a class="reference internal" href="#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> using <tt class="docutils literal"><span class="pre">\x</span></tt>, <tt class="docutils literal"><span class="pre">\u</span></tt> or <tt class="docutils literal"><span class="pre">\U</span></tt> escapes.  This generates a string
similar to that returned by <a class="reference internal" href="#repr" title="repr"><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> in Python 2.</p>
</dd></dl>

<dl class="function">
<dt id="bin">
<tt class="descname">bin</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#bin" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert an integer number to a binary string. The result is a valid Python
expression.  If <em>x</em> is not a Python <a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> object, it has to define an
<a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><tt class="xref py py-meth docutils literal"><span class="pre">__index__()</span></tt></a> method that returns an integer.</p>
</dd></dl>

<dl class="class">
<dt id="bool">
<em class="property">class </em><tt class="descname">bool</tt><big>(</big><span class="optional">[</span><em>x</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#bool" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a Boolean value, i.e. one of <tt class="docutils literal"><span class="pre">True</span></tt> or <tt class="docutils literal"><span class="pre">False</span></tt>.  <em>x</em> is converted
using the standard <a class="reference internal" href="stdtypes.html#truth"><em>truth testing procedure</em></a>.  If <em>x</em> is false
or omitted, this returns <tt class="docutils literal"><span class="pre">False</span></tt>; otherwise it returns <tt class="docutils literal"><span class="pre">True</span></tt>.  The
<a class="reference internal" href="#bool" title="bool"><tt class="xref py py-class docutils literal"><span class="pre">bool</span></tt></a> class is a subclass of <a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> (see <a class="reference internal" href="stdtypes.html#typesnumeric"><em>Numeric Types &#8212; int, float, complex</em></a>).
It cannot be subclassed further.  Its only instances are <tt class="docutils literal"><span class="pre">False</span></tt> and
<tt class="docutils literal"><span class="pre">True</span></tt> (see <a class="reference internal" href="stdtypes.html#bltin-boolean-values"><em>Boolean Values</em></a>).</p>
</dd></dl>

<span class="target" id="func-bytearray"><span id="index-0"></span></span><dl class="class">
<dt id="bytearray">
<em class="property">class </em><tt class="descname">bytearray</tt><big>(</big><span class="optional">[</span><em>source</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>errors</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#bytearray" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new array of bytes.  The <a class="reference internal" href="#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> class is a mutable
sequence of integers in the range 0 &lt;= x &lt; 256.  It has most of the usual
methods of mutable sequences, described in <a class="reference internal" href="stdtypes.html#typesseq-mutable"><em>Mutable Sequence Types</em></a>, as well
as most methods that the <a class="reference internal" href="#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> type has, see <a class="reference internal" href="stdtypes.html#bytes-methods"><em>Bytes and Bytearray Operations</em></a>.</p>
<p>The optional <em>source</em> parameter can be used to initialize the array in a few
different ways:</p>
<ul class="simple">
<li>If it is a <em>string</em>, you must also give the <em>encoding</em> (and optionally,
<em>errors</em>) parameters; <a class="reference internal" href="#bytearray" title="bytearray"><tt class="xref py py-func docutils literal"><span class="pre">bytearray()</span></tt></a> then converts the string to
bytes using <a class="reference internal" href="stdtypes.html#str.encode" title="str.encode"><tt class="xref py py-meth docutils literal"><span class="pre">str.encode()</span></tt></a>.</li>
<li>If it is an <em>integer</em>, the array will have that size and will be
initialized with null bytes.</li>
<li>If it is an object conforming to the <em>buffer</em> interface, a read-only buffer
of the object will be used to initialize the bytes array.</li>
<li>If it is an <em>iterable</em>, it must be an iterable of integers in the range
<tt class="docutils literal"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">256</span></tt>, which are used as the initial contents of the array.</li>
</ul>
<p>Without an argument, an array of size 0 is created.</p>
<p>See also <a class="reference internal" href="stdtypes.html#binaryseq"><em>Binary Sequence Types &#8212; bytes, bytearray, memoryview</em></a> and <a class="reference internal" href="stdtypes.html#typebytearray"><em>Bytearray Objects</em></a>.</p>
</dd></dl>

<span class="target" id="func-bytes"></span><dl class="class">
<dt id="bytes">
<em class="property">class </em><tt class="descname">bytes</tt><big>(</big><span class="optional">[</span><em>source</em><span class="optional">[</span>, <em>encoding</em><span class="optional">[</span>, <em>errors</em><span class="optional">]</span><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#bytes" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new &#8220;bytes&#8221; object, which is an immutable sequence of integers in
the range <tt class="docutils literal"><span class="pre">0</span> <span class="pre">&lt;=</span> <span class="pre">x</span> <span class="pre">&lt;</span> <span class="pre">256</span></tt>.  <a class="reference internal" href="#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> is an immutable version of
<a class="reference internal" href="#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> &#8211; it has the same non-mutating methods and the same
indexing and slicing behavior.</p>
<p>Accordingly, constructor arguments are interpreted as for <a class="reference internal" href="#bytearray" title="bytearray"><tt class="xref py py-func docutils literal"><span class="pre">bytearray()</span></tt></a>.</p>
<p>Bytes objects can also be created with literals, see <a class="reference internal" href="../reference/lexical_analysis.html#strings"><em>String and Bytes literals</em></a>.</p>
<p>See also <a class="reference internal" href="stdtypes.html#binaryseq"><em>Binary Sequence Types &#8212; bytes, bytearray, memoryview</em></a>, <a class="reference internal" href="stdtypes.html#typebytes"><em>Bytes</em></a>, and <a class="reference internal" href="stdtypes.html#bytes-methods"><em>Bytes and Bytearray Operations</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="callable">
<tt class="descname">callable</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#callable" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <a class="reference internal" href="constants.html#True" title="True"><tt class="xref py py-const docutils literal"><span class="pre">True</span></tt></a> if the <em>object</em> argument appears callable,
<a class="reference internal" href="constants.html#False" title="False"><tt class="xref py py-const docutils literal"><span class="pre">False</span></tt></a> if not.  If this returns true, it is still possible that a
call fails, but if it is false, calling <em>object</em> will never succeed.
Note that classes are callable (calling a class returns a new instance);
instances are callable if their class has a <a class="reference internal" href="../reference/datamodel.html#object.__call__" title="object.__call__"><tt class="xref py py-meth docutils literal"><span class="pre">__call__()</span></tt></a> method.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2: </span>This function was first removed in Python 3.0 and then brought back
in Python 3.2.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="chr">
<tt class="descname">chr</tt><big>(</big><em>i</em><big>)</big><a class="headerlink" href="#chr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the string representing a character whose Unicode code point is the integer
<em>i</em>.  For example, <tt class="docutils literal"><span class="pre">chr(97)</span></tt> returns the string <tt class="docutils literal"><span class="pre">'a'</span></tt>. This is the
inverse of <a class="reference internal" href="#ord" title="ord"><tt class="xref py py-func docutils literal"><span class="pre">ord()</span></tt></a>.  The valid range for the argument is from 0 through
1,114,111 (0x10FFFF in base 16).  <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> will be raised if <em>i</em> is
outside that range.</p>
</dd></dl>

<dl class="function">
<dt id="classmethod">
<tt class="descname">classmethod</tt><big>(</big><em>function</em><big>)</big><a class="headerlink" href="#classmethod" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a class method for <em>function</em>.</p>
<p>A class method receives the class as implicit first argument, just like an
instance method receives the instance. To declare a class method, use this
idiom:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
    <span class="nd">@classmethod</span>
    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">cls</span><span class="p">,</span> <span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span> <span class="o">...</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">&#64;classmethod</span></tt> form is a function <a class="reference internal" href="../glossary.html#term-decorator"><em class="xref std std-term">decorator</em></a> &#8211; see the description
of function definitions in <a class="reference internal" href="../reference/compound_stmts.html#function"><em>Function definitions</em></a> for details.</p>
<p>It can be called either on the class (such as <tt class="docutils literal"><span class="pre">C.f()</span></tt>) or on an instance (such
as <tt class="docutils literal"><span class="pre">C().f()</span></tt>).  The instance is ignored except for its class. If a class
method is called for a derived class, the derived class object is passed as the
implied first argument.</p>
<p>Class methods are different than C++ or Java static methods. If you want those,
see <a class="reference internal" href="#staticmethod" title="staticmethod"><tt class="xref py py-func docutils literal"><span class="pre">staticmethod()</span></tt></a> in this section.</p>
<p>For more information on class methods, consult the documentation on the standard
type hierarchy in <a class="reference internal" href="../reference/datamodel.html#types"><em>The standard type hierarchy</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="compile">
<tt class="descname">compile</tt><big>(</big><em>source</em>, <em>filename</em>, <em>mode</em>, <em>flags=0</em>, <em>dont_inherit=False</em>, <em>optimize=-1</em><big>)</big><a class="headerlink" href="#compile" title="Permalink to this definition">¶</a></dt>
<dd><p>Compile the <em>source</em> into a code or AST object.  Code objects can be executed
by <a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a> or <a class="reference internal" href="#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a>.  <em>source</em> can either be a normal string, a
byte string, or an AST object.  Refer to the <a class="reference internal" href="ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><tt class="xref py py-mod docutils literal"><span class="pre">ast</span></tt></a> module documentation
for information on how to work with AST objects.</p>
<p>The <em>filename</em> argument should give the file from which the code was read;
pass some recognizable value if it wasn&#8217;t read from a file (<tt class="docutils literal"><span class="pre">'&lt;string&gt;'</span></tt> is
commonly used).</p>
<p>The <em>mode</em> argument specifies what kind of code must be compiled; it can be
<tt class="docutils literal"><span class="pre">'exec'</span></tt> if <em>source</em> consists of a sequence of statements, <tt class="docutils literal"><span class="pre">'eval'</span></tt> if it
consists of a single expression, or <tt class="docutils literal"><span class="pre">'single'</span></tt> if it consists of a single
interactive statement (in the latter case, expression statements that
evaluate to something other than <tt class="docutils literal"><span class="pre">None</span></tt> will be printed).</p>
<p>The optional arguments <em>flags</em> and <em>dont_inherit</em> control which future
statements (see <span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0236"><strong>PEP 236</strong></a>) affect the compilation of <em>source</em>.  If neither
is present (or both are zero) the code is compiled with those future
statements that are in effect in the code that is calling <a class="reference internal" href="#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a>.  If the
<em>flags</em> argument is given and <em>dont_inherit</em> is not (or is zero) then the
future statements specified by the <em>flags</em> argument are used in addition to
those that would be used anyway. If <em>dont_inherit</em> is a non-zero integer then
the <em>flags</em> argument is it &#8211; the future statements in effect around the call
to compile are ignored.</p>
<p>Future statements are specified by bits which can be bitwise ORed together to
specify multiple statements.  The bitfield required to specify a given feature
can be found as the <tt class="xref py py-attr docutils literal"><span class="pre">compiler_flag</span></tt> attribute on
the <tt class="xref py py-class docutils literal"><span class="pre">_Feature</span></tt> instance in the <a class="reference internal" href="__future__.html#module-__future__" title="__future__: Future statement definitions"><tt class="xref py py-mod docutils literal"><span class="pre">__future__</span></tt></a> module.</p>
<p>The argument <em>optimize</em> specifies the optimization level of the compiler; the
default value of <tt class="docutils literal"><span class="pre">-1</span></tt> selects the optimization level of the interpreter as
given by <a class="reference internal" href="../using/cmdline.html#cmdoption-O"><em class="xref std std-option">-O</em></a> options.  Explicit levels are <tt class="docutils literal"><span class="pre">0</span></tt> (no optimization;
<tt class="docutils literal"><span class="pre">__debug__</span></tt> is true), <tt class="docutils literal"><span class="pre">1</span></tt> (asserts are removed, <tt class="docutils literal"><span class="pre">__debug__</span></tt> is false)
or <tt class="docutils literal"><span class="pre">2</span></tt> (docstrings are removed too).</p>
<p>This function raises <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><tt class="xref py py-exc docutils literal"><span class="pre">SyntaxError</span></tt></a> if the compiled source is invalid,
and <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> if the source contains null bytes.</p>
<p>If you want to parse Python code into its AST representation, see
<a class="reference internal" href="ast.html#ast.parse" title="ast.parse"><tt class="xref py py-func docutils literal"><span class="pre">ast.parse()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When compiling a string with multi-line code in <tt class="docutils literal"><span class="pre">'single'</span></tt> or
<tt class="docutils literal"><span class="pre">'eval'</span></tt> mode, input must be terminated by at least one newline
character.  This is to facilitate detection of incomplete and complete
statements in the <a class="reference internal" href="code.html#module-code" title="code: Facilities to implement read-eval-print loops."><tt class="xref py py-mod docutils literal"><span class="pre">code</span></tt></a> module.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>Allowed use of Windows and Mac newlines.  Also input in <tt class="docutils literal"><span class="pre">'exec'</span></tt> mode
does not have to end in a newline anymore.  Added the <em>optimize</em> parameter.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="complex">
<em class="property">class </em><tt class="descname">complex</tt><big>(</big><span class="optional">[</span><em>real</em><span class="optional">[</span>, <em>imag</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#complex" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a complex number with the value <em>real</em> + <em>imag</em>*1j or convert a string
or number to a complex number.  If the first parameter is a string, it will
be interpreted as a complex number and the function must be called without a
second parameter.  The second parameter can never be a string. Each argument
may be any numeric type (including complex).  If <em>imag</em> is omitted, it
defaults to zero and the constructor serves as a numeric conversion like
<a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> and <a class="reference internal" href="#float" title="float"><tt class="xref py py-class docutils literal"><span class="pre">float</span></tt></a>.  If both arguments are omitted, returns
<tt class="docutils literal"><span class="pre">0j</span></tt>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">When converting from a string, the string must not contain whitespace
around the central <tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt> operator.  For example,
<tt class="docutils literal"><span class="pre">complex('1+2j')</span></tt> is fine, but <tt class="docutils literal"><span class="pre">complex('1</span> <span class="pre">+</span> <span class="pre">2j')</span></tt> raises
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>.</p>
</div>
<p>The complex type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><em>Numeric Types &#8212; int, float, complex</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="delattr">
<tt class="descname">delattr</tt><big>(</big><em>object</em>, <em>name</em><big>)</big><a class="headerlink" href="#delattr" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a relative of <a class="reference internal" href="#setattr" title="setattr"><tt class="xref py py-func docutils literal"><span class="pre">setattr()</span></tt></a>.  The arguments are an object and a
string.  The string must be the name of one of the object&#8217;s attributes.  The
function deletes the named attribute, provided the object allows it.  For
example, <tt class="docutils literal"><span class="pre">delattr(x,</span> <span class="pre">'foobar')</span></tt> is equivalent to <tt class="docutils literal"><span class="pre">del</span> <span class="pre">x.foobar</span></tt>.</p>
</dd></dl>

<span class="target" id="func-dict"></span><dl class="class">
<dt>
<em class="property">class </em><tt class="descname">dict</tt><big>(</big><em>**kwarg</em><big>)</big></dt>
<dt>
<em class="property">class </em><tt class="descname">dict</tt><big>(</big><em>mapping</em>, <em>**kwarg</em><big>)</big></dt>
<dt>
<em class="property">class </em><tt class="descname">dict</tt><big>(</big><em>iterable</em>, <em>**kwarg</em><big>)</big></dt>
<dd><p>Create a new dictionary.  The <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> object is the dictionary class.
See <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> and <a class="reference internal" href="stdtypes.html#typesmapping"><em>Mapping Types &#8212; dict</em></a> for documentation about this class.</p>
<p>For other containers see the built-in <a class="reference internal" href="stdtypes.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>, <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a>, and
<a class="reference internal" href="stdtypes.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a> classes, as well as the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module.</p>
</dd></dl>

<dl class="function">
<dt id="dir">
<tt class="descname">dir</tt><big>(</big><span class="optional">[</span><em>object</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#dir" title="Permalink to this definition">¶</a></dt>
<dd><p>Without arguments, return the list of names in the current local scope.  With an
argument, attempt to return a list of valid attributes for that object.</p>
<p>If the object has a method named <a class="reference internal" href="../reference/datamodel.html#object.__dir__" title="object.__dir__"><tt class="xref py py-meth docutils literal"><span class="pre">__dir__()</span></tt></a>, this method will be called and
must return the list of attributes. This allows objects that implement a custom
<a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-func docutils literal"><span class="pre">__getattr__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><tt class="xref py py-func docutils literal"><span class="pre">__getattribute__()</span></tt></a> function to customize the way
<a class="reference internal" href="#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a> reports their attributes.</p>
<p>If the object does not provide <a class="reference internal" href="../reference/datamodel.html#object.__dir__" title="object.__dir__"><tt class="xref py py-meth docutils literal"><span class="pre">__dir__()</span></tt></a>, the function tries its best to
gather information from the object&#8217;s <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt> attribute, if defined, and
from its type object.  The resulting list is not necessarily complete, and may
be inaccurate when the object has a custom <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-func docutils literal"><span class="pre">__getattr__()</span></tt></a>.</p>
<p>The default <a class="reference internal" href="#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a> mechanism behaves differently with different types of
objects, as it attempts to produce the most relevant, rather than complete,
information:</p>
<ul class="simple">
<li>If the object is a module object, the list contains the names of the module&#8217;s
attributes.</li>
<li>If the object is a type or class object, the list contains the names of its
attributes, and recursively of the attributes of its bases.</li>
<li>Otherwise, the list contains the object&#8217;s attributes&#8217; names, the names of its
class&#8217;s attributes, and recursively of the attributes of its class&#8217;s base
classes.</li>
</ul>
<p>The resulting list is sorted alphabetically.  For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">struct</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">()</span>   <span class="c"># show the names in the module namespace</span>
<span class="go">[&#39;__builtins__&#39;, &#39;__name__&#39;, &#39;struct&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">struct</span><span class="p">)</span>   <span class="c"># show the names in the struct module </span>
<span class="go">[&#39;Struct&#39;, &#39;__all__&#39;, &#39;__builtins__&#39;, &#39;__cached__&#39;, &#39;__doc__&#39;, &#39;__file__&#39;,</span>
<span class="go"> &#39;__initializing__&#39;, &#39;__loader__&#39;, &#39;__name__&#39;, &#39;__package__&#39;,</span>
<span class="go"> &#39;_clearcache&#39;, &#39;calcsize&#39;, &#39;error&#39;, &#39;pack&#39;, &#39;pack_into&#39;,</span>
<span class="go"> &#39;unpack&#39;, &#39;unpack_from&#39;]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Shape</span><span class="p">:</span>
<span class="gp">... </span>    <span class="k">def</span> <span class="nf">__dir__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="gp">... </span>        <span class="k">return</span> <span class="p">[</span><span class="s">&#39;area&#39;</span><span class="p">,</span> <span class="s">&#39;perimeter&#39;</span><span class="p">,</span> <span class="s">&#39;location&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="n">Shape</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">dir</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="go">[&#39;area&#39;, &#39;location&#39;, &#39;perimeter&#39;]</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Because <a class="reference internal" href="#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a> is supplied primarily as a convenience for use at an
interactive prompt, it tries to supply an interesting set of names more
than it tries to supply a rigorously or consistently defined set of names,
and its detailed behavior may change across releases.  For example,
metaclass attributes are not in the result list when the argument is a
class.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="divmod">
<tt class="descname">divmod</tt><big>(</big><em>a</em>, <em>b</em><big>)</big><a class="headerlink" href="#divmod" title="Permalink to this definition">¶</a></dt>
<dd><p>Take two (non complex) numbers as arguments and return a pair of numbers
consisting of their quotient and remainder when using integer division.  With
mixed operand types, the rules for binary arithmetic operators apply.  For
integers, the result is the same as <tt class="docutils literal"><span class="pre">(a</span> <span class="pre">//</span> <span class="pre">b,</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b)</span></tt>. For floating point
numbers the result is <tt class="docutils literal"><span class="pre">(q,</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b)</span></tt>, where <em>q</em> is usually <tt class="docutils literal"><span class="pre">math.floor(a</span> <span class="pre">/</span>
<span class="pre">b)</span></tt> but may be 1 less than that.  In any case <tt class="docutils literal"><span class="pre">q</span> <span class="pre">*</span> <span class="pre">b</span> <span class="pre">+</span> <span class="pre">a</span> <span class="pre">%</span> <span class="pre">b</span></tt> is very
close to <em>a</em>, if <tt class="docutils literal"><span class="pre">a</span> <span class="pre">%</span> <span class="pre">b</span></tt> is non-zero it has the same sign as <em>b</em>, and <tt class="docutils literal"><span class="pre">0</span>
<span class="pre">&lt;=</span> <span class="pre">abs(a</span> <span class="pre">%</span> <span class="pre">b)</span> <span class="pre">&lt;</span> <span class="pre">abs(b)</span></tt>.</p>
</dd></dl>

<dl class="function">
<dt id="enumerate">
<tt class="descname">enumerate</tt><big>(</big><em>iterable</em>, <em>start=0</em><big>)</big><a class="headerlink" href="#enumerate" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an enumerate object. <em>iterable</em> must be a sequence, an
<a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a>, or some other object which supports iteration.
The <a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><tt class="xref py py-meth docutils literal"><span class="pre">__next__()</span></tt></a> method of the iterator returned by
<a class="reference internal" href="#enumerate" title="enumerate"><tt class="xref py py-func docutils literal"><span class="pre">enumerate()</span></tt></a> returns a tuple containing a count (from <em>start</em> which
defaults to 0) and the values obtained from iterating over <em>iterable</em>.</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">seasons</span> <span class="o">=</span> <span class="p">[</span><span class="s">&#39;Spring&#39;</span><span class="p">,</span> <span class="s">&#39;Summer&#39;</span><span class="p">,</span> <span class="s">&#39;Fall&#39;</span><span class="p">,</span> <span class="s">&#39;Winter&#39;</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seasons</span><span class="p">))</span>
<span class="go">[(0, &#39;Spring&#39;), (1, &#39;Summer&#39;), (2, &#39;Fall&#39;), (3, &#39;Winter&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="nb">enumerate</span><span class="p">(</span><span class="n">seasons</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
<span class="go">[(1, &#39;Spring&#39;), (2, &#39;Summer&#39;), (3, &#39;Fall&#39;), (4, &#39;Winter&#39;)]</span>
</pre></div>
</div>
<p>Equivalent to:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">enumerate</span><span class="p">(</span><span class="n">sequence</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="n">n</span> <span class="o">=</span> <span class="n">start</span>
    <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">sequence</span><span class="p">:</span>
        <span class="k">yield</span> <span class="n">n</span><span class="p">,</span> <span class="n">elem</span>
        <span class="n">n</span> <span class="o">+=</span> <span class="mi">1</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="eval">
<tt class="descname">eval</tt><big>(</big><em>expression</em>, <em>globals=None</em>, <em>locals=None</em><big>)</big><a class="headerlink" href="#eval" title="Permalink to this definition">¶</a></dt>
<dd><p>The arguments are a string and optional globals and locals.  If provided,
<em>globals</em> must be a dictionary.  If provided, <em>locals</em> can be any mapping
object.</p>
<p>The <em>expression</em> argument is parsed and evaluated as a Python expression
(technically speaking, a condition list) using the <em>globals</em> and <em>locals</em>
dictionaries as global and local namespace.  If the <em>globals</em> dictionary is
present and lacks &#8216;__builtins__&#8217;, the current globals are copied into <em>globals</em>
before <em>expression</em> is parsed.  This means that <em>expression</em> normally has full
access to the standard <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><tt class="xref py py-mod docutils literal"><span class="pre">builtins</span></tt></a> module and restricted environments are
propagated.  If the <em>locals</em> dictionary is omitted it defaults to the <em>globals</em>
dictionary.  If both dictionaries are omitted, the expression is executed in the
environment where <a class="reference internal" href="#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a> is called.  The return value is the result of
the evaluated expression. Syntax errors are reported as exceptions.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">eval</span><span class="p">(</span><span class="s">&#39;x+1&#39;</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
<p>This function can also be used to execute arbitrary code objects (such as
those created by <a class="reference internal" href="#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a>).  In this case pass a code object instead
of a string.  If the code object has been compiled with <tt class="docutils literal"><span class="pre">'exec'</span></tt> as the
<em>mode</em> argument, <a class="reference internal" href="#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a>&#8216;s return value will be <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>Hints: dynamic execution of statements is supported by the <a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a>
function.  The <a class="reference internal" href="#globals" title="globals"><tt class="xref py py-func docutils literal"><span class="pre">globals()</span></tt></a> and <a class="reference internal" href="#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a> functions
returns the current global and local dictionary, respectively, which may be
useful to pass around for use by <a class="reference internal" href="#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a> or <a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a>.</p>
<p>See <a class="reference internal" href="ast.html#ast.literal_eval" title="ast.literal_eval"><tt class="xref py py-func docutils literal"><span class="pre">ast.literal_eval()</span></tt></a> for a function that can safely evaluate strings
with expressions containing only literals.</p>
</dd></dl>

<span class="target" id="index-2"></span><dl class="function">
<dt id="exec">
<tt class="descname">exec</tt><big>(</big><em>object</em><span class="optional">[</span>, <em>globals</em><span class="optional">[</span>, <em>locals</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#exec" title="Permalink to this definition">¶</a></dt>
<dd><p>This function supports dynamic execution of Python code. <em>object</em> must be
either a string or a code object.  If it is a string, the string is parsed as
a suite of Python statements which is then executed (unless a syntax error
occurs). <a class="footnote-reference" href="#id2" id="id1">[1]</a> If it is a code object, it is simply executed.  In all cases,
the code that&#8217;s executed is expected to be valid as file input (see the
section &#8220;File input&#8221; in the Reference Manual). Be aware that the
<a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> and <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> statements may not be used outside of
function definitions even within the context of code passed to the
<a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a> function. The return value is <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>In all cases, if the optional parts are omitted, the code is executed in the
current scope.  If only <em>globals</em> is provided, it must be a dictionary, which
will be used for both the global and the local variables.  If <em>globals</em> and
<em>locals</em> are given, they are used for the global and local variables,
respectively.  If provided, <em>locals</em> can be any mapping object.  Remember
that at module level, globals and locals are the same dictionary. If exec
gets two separate objects as <em>globals</em> and <em>locals</em>, the code will be
executed as if it were embedded in a class definition.</p>
<p>If the <em>globals</em> dictionary does not contain a value for the key
<tt class="docutils literal"><span class="pre">__builtins__</span></tt>, a reference to the dictionary of the built-in module
<a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><tt class="xref py py-mod docutils literal"><span class="pre">builtins</span></tt></a> is inserted under that key.  That way you can control what
builtins are available to the executed code by inserting your own
<tt class="docutils literal"><span class="pre">__builtins__</span></tt> dictionary into <em>globals</em> before passing it to <a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The built-in functions <a class="reference internal" href="#globals" title="globals"><tt class="xref py py-func docutils literal"><span class="pre">globals()</span></tt></a> and <a class="reference internal" href="#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a> return the current
global and local dictionary, respectively, which may be useful to pass around
for use as the second and third argument to <a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The default <em>locals</em> act as described for function <a class="reference internal" href="#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a> below:
modifications to the default <em>locals</em> dictionary should not be attempted.
Pass an explicit <em>locals</em> dictionary if you need to see effects of the
code on <em>locals</em> after function <a class="reference internal" href="#exec" title="exec"><tt class="xref py py-func docutils literal"><span class="pre">exec()</span></tt></a> returns.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="filter">
<tt class="descname">filter</tt><big>(</big><em>function</em>, <em>iterable</em><big>)</big><a class="headerlink" href="#filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Construct an iterator from those elements of <em>iterable</em> for which <em>function</em>
returns true.  <em>iterable</em> may be either a sequence, a container which
supports iteration, or an iterator.  If <em>function</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, the identity
function is assumed, that is, all elements of <em>iterable</em> that are false are
removed.</p>
<p>Note that <tt class="docutils literal"><span class="pre">filter(function,</span> <span class="pre">iterable)</span></tt> is equivalent to the generator
expression <tt class="docutils literal"><span class="pre">(item</span> <span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">iterable</span> <span class="pre">if</span> <span class="pre">function(item))</span></tt> if function is
not <tt class="docutils literal"><span class="pre">None</span></tt> and <tt class="docutils literal"><span class="pre">(item</span> <span class="pre">for</span> <span class="pre">item</span> <span class="pre">in</span> <span class="pre">iterable</span> <span class="pre">if</span> <span class="pre">item)</span></tt> if function is
<tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<p>See <a class="reference internal" href="itertools.html#itertools.filterfalse" title="itertools.filterfalse"><tt class="xref py py-func docutils literal"><span class="pre">itertools.filterfalse()</span></tt></a> for the complementary function that returns
elements of <em>iterable</em> for which <em>function</em> returns false.</p>
</dd></dl>

<dl class="class">
<dt id="float">
<em class="property">class </em><tt class="descname">float</tt><big>(</big><span class="optional">[</span><em>x</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#float" title="Permalink to this definition">¶</a></dt>
<dd><p id="index-3">Return a floating point number constructed from a number or string <em>x</em>.</p>
<p>If the argument is a string, it should contain a decimal number, optionally
preceded by a sign, and optionally embedded in whitespace.  The optional
sign may be <tt class="docutils literal"><span class="pre">'+'</span></tt> or <tt class="docutils literal"><span class="pre">'-'</span></tt>; a <tt class="docutils literal"><span class="pre">'+'</span></tt> sign has no effect on the value
produced.  The argument may also be a string representing a NaN
(not-a-number), or a positive or negative infinity.  More precisely, the
input must conform to the following grammar after leading and trailing
whitespace characters are removed:</p>
<pre>
<strong id="grammar-token-sign">sign          </strong> ::=  &quot;+&quot; | &quot;-&quot;
<strong id="grammar-token-infinity">infinity      </strong> ::=  &quot;Infinity&quot; | &quot;inf&quot;
<strong id="grammar-token-nan">nan           </strong> ::=  &quot;nan&quot;
<strong id="grammar-token-numeric_value">numeric_value </strong> ::=  <a class="reference internal" href="../reference/lexical_analysis.html#grammar-token-floatnumber"><tt class="xref docutils literal"><span class="pre">floatnumber</span></tt></a> | <a class="reference internal" href="#grammar-token-infinity"><tt class="xref docutils literal"><span class="pre">infinity</span></tt></a> | <a class="reference internal" href="#grammar-token-nan"><tt class="xref docutils literal"><span class="pre">nan</span></tt></a>
<strong id="grammar-token-numeric_string">numeric_string</strong> ::=  [<a class="reference internal" href="#grammar-token-sign"><tt class="xref docutils literal"><span class="pre">sign</span></tt></a>] <a class="reference internal" href="#grammar-token-numeric_value"><tt class="xref docutils literal"><span class="pre">numeric_value</span></tt></a>
</pre>
<p>Here <tt class="docutils literal"><span class="pre">floatnumber</span></tt> is the form of a Python floating-point literal,
described in <a class="reference internal" href="../reference/lexical_analysis.html#floating"><em>Floating point literals</em></a>.  Case is not significant, so, for example,
&#8220;inf&#8221;, &#8220;Inf&#8221;, &#8220;INFINITY&#8221; and &#8220;iNfINity&#8221; are all acceptable spellings for
positive infinity.</p>
<p>Otherwise, if the argument is an integer or a floating point number, a
floating point number with the same value (within Python&#8217;s floating point
precision) is returned.  If the argument is outside the range of a Python
float, an <a class="reference internal" href="exceptions.html#OverflowError" title="OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a> will be raised.</p>
<p>For a general Python object <tt class="docutils literal"><span class="pre">x</span></tt>, <tt class="docutils literal"><span class="pre">float(x)</span></tt> delegates to
<tt class="docutils literal"><span class="pre">x.__float__()</span></tt>.</p>
<p>If no argument is given, <tt class="docutils literal"><span class="pre">0.0</span></tt> is returned.</p>
<p>Examples:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s">&#39;+1.23&#39;</span><span class="p">)</span>
<span class="go">1.23</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s">&#39;   -12345</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
<span class="go">-12345.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s">&#39;1e-003&#39;</span><span class="p">)</span>
<span class="go">0.001</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s">&#39;+1E6&#39;</span><span class="p">)</span>
<span class="go">1000000.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="s">&#39;-Infinity&#39;</span><span class="p">)</span>
<span class="go">-inf</span>
</pre></div>
</div>
<p>The float type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><em>Numeric Types &#8212; int, float, complex</em></a>.</p>
<span class="target" id="index-4"></span></dd></dl>

<dl class="function">
<dt id="format">
<tt class="descname">format</tt><big>(</big><em>value</em><span class="optional">[</span>, <em>format_spec</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#format" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert a <em>value</em> to a &#8220;formatted&#8221; representation, as controlled by
<em>format_spec</em>.  The interpretation of <em>format_spec</em> will depend on the type
of the <em>value</em> argument, however there is a standard formatting syntax that
is used by most built-in types: <a class="reference internal" href="string.html#formatspec"><em>Format Specification Mini-Language</em></a>.</p>
<p>The default <em>format_spec</em> is an empty string which usually gives the same
effect as calling <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str(value)</span></tt></a>.</p>
<p>A call to <tt class="docutils literal"><span class="pre">format(value,</span> <span class="pre">format_spec)</span></tt> is translated to
<tt class="docutils literal"><span class="pre">type(value).__format__(value,</span> <span class="pre">format_spec)</span></tt> which bypasses the instance
dictionary when searching for the value&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__format__" title="object.__format__"><tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt></a> method.  A
<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised if the method search reaches
<a class="reference internal" href="#object" title="object"><tt class="xref py py-mod docutils literal"><span class="pre">object</span></tt></a> and the <em>format_spec</em> is non-empty, or if either the
<em>format_spec</em> or the return value are not strings.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span><tt class="docutils literal"><span class="pre">object().__format__(format_spec)</span></tt> raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>
if <em>format_spec</em> is not an empty string.</p>
</div>
</dd></dl>

<span class="target" id="func-frozenset"></span><dl class="class">
<dt>
<em class="property">class </em><tt class="descname">frozenset</tt><big>(</big><span class="optional">[</span><em>iterable</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Return a new <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><tt class="xref py py-class docutils literal"><span class="pre">frozenset</span></tt></a> object, optionally with elements taken from
<em>iterable</em>.  <tt class="docutils literal"><span class="pre">frozenset</span></tt> is a built-in class.  See <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><tt class="xref py py-class docutils literal"><span class="pre">frozenset</span></tt></a> and
<a class="reference internal" href="stdtypes.html#types-set"><em>Set Types &#8212; set, frozenset</em></a> for documentation about this class.</p>
<p>For other containers see the built-in <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a>, <a class="reference internal" href="stdtypes.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>,
<a class="reference internal" href="stdtypes.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a>, and <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> classes, as well as the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a>
module.</p>
</dd></dl>

<dl class="function">
<dt id="getattr">
<tt class="descname">getattr</tt><big>(</big><em>object</em>, <em>name</em><span class="optional">[</span>, <em>default</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#getattr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the value of the named attribute of <em>object</em>.  <em>name</em> must be a string.
If the string is the name of one of the object&#8217;s attributes, the result is the
value of that attribute.  For example, <tt class="docutils literal"><span class="pre">getattr(x,</span> <span class="pre">'foobar')</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">x.foobar</span></tt>.  If the named attribute does not exist, <em>default</em> is returned if
provided, otherwise <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a> is raised.</p>
</dd></dl>

<dl class="function">
<dt id="globals">
<tt class="descname">globals</tt><big>(</big><big>)</big><a class="headerlink" href="#globals" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a dictionary representing the current global symbol table. This is always
the dictionary of the current module (inside a function or method, this is the
module where it is defined, not the module from which it is called).</p>
</dd></dl>

<dl class="function">
<dt id="hasattr">
<tt class="descname">hasattr</tt><big>(</big><em>object</em>, <em>name</em><big>)</big><a class="headerlink" href="#hasattr" title="Permalink to this definition">¶</a></dt>
<dd><p>The arguments are an object and a string.  The result is <tt class="docutils literal"><span class="pre">True</span></tt> if the
string is the name of one of the object&#8217;s attributes, <tt class="docutils literal"><span class="pre">False</span></tt> if not. (This
is implemented by calling <tt class="docutils literal"><span class="pre">getattr(object,</span> <span class="pre">name)</span></tt> and seeing whether it
raises an <a class="reference internal" href="exceptions.html#AttributeError" title="AttributeError"><tt class="xref py py-exc docutils literal"><span class="pre">AttributeError</span></tt></a> or not.)</p>
</dd></dl>

<dl class="function">
<dt id="hash">
<tt class="descname">hash</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#hash" title="Permalink to this definition">¶</a></dt>
<dd><blockquote>
<div>Return the hash value of the object (if it has one).  Hash values are
integers.  They are used to quickly compare dictionary keys during a
dictionary lookup.  Numeric values that compare equal have the same hash
value (even if they are of different types, as is the case for 1 and 1.0).</div></blockquote>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">For object&#8217;s with custom <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a> methods, note that <a class="reference internal" href="#hash" title="hash"><tt class="xref py py-func docutils literal"><span class="pre">hash()</span></tt></a>
truncates the return value based on the bit width of the host machine.
See <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a> for details.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="help">
<tt class="descname">help</tt><big>(</big><span class="optional">[</span><em>object</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#help" title="Permalink to this definition">¶</a></dt>
<dd><p>Invoke the built-in help system.  (This function is intended for interactive
use.)  If no argument is given, the interactive help system starts on the
interpreter console.  If the argument is a string, then the string is looked up
as the name of a module, function, class, method, keyword, or documentation
topic, and a help page is printed on the console.  If the argument is any other
kind of object, a help page on the object is generated.</p>
<p>This function is added to the built-in namespace by the <a class="reference internal" href="site.html#module-site" title="site: Module responsible for site-specific configuration."><tt class="xref py py-mod docutils literal"><span class="pre">site</span></tt></a> module.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>Changes to <a class="reference internal" href="pydoc.html#module-pydoc" title="pydoc: Documentation generator and online help system."><tt class="xref py py-mod docutils literal"><span class="pre">pydoc</span></tt></a> and <a class="reference internal" href="inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><tt class="xref py py-mod docutils literal"><span class="pre">inspect</span></tt></a> mean that the reported
signatures for callables are now more comprehensive and consistent.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="hex">
<tt class="descname">hex</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#hex" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert an integer number to a lowercase hexadecimal string
prefixed with &#8220;0x&#8221;, for example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">hex</span><span class="p">(</span><span class="mi">255</span><span class="p">)</span>
<span class="go">&#39;0xff&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hex</span><span class="p">(</span><span class="o">-</span><span class="mi">42</span><span class="p">)</span>
<span class="go">&#39;-0x2a&#39;</span>
</pre></div>
</div>
<p>If x is not a Python <a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> object, it has to define an __index__()
method that returns an integer.</p>
<p>See also <a class="reference internal" href="#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> for converting a hexadecimal string to an
integer using a base of 16.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">To obtain a hexadecimal string representation for a float, use the
<a class="reference internal" href="stdtypes.html#float.hex" title="float.hex"><tt class="xref py py-meth docutils literal"><span class="pre">float.hex()</span></tt></a> method.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="id">
<tt class="descname">id</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#id" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the &#8220;identity&#8221; of an object.  This is an integer which
is guaranteed to be unique and constant for this object during its lifetime.
Two objects with non-overlapping lifetimes may have the same <a class="reference internal" href="#id" title="id"><tt class="xref py py-func docutils literal"><span class="pre">id()</span></tt></a>
value.</p>
<div class="impl-detail compound">
<p><strong>CPython implementation detail:</strong> This is the address of the object in memory.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="input">
<tt class="descname">input</tt><big>(</big><span class="optional">[</span><em>prompt</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#input" title="Permalink to this definition">¶</a></dt>
<dd><p>If the <em>prompt</em> argument is present, it is written to standard output without
a trailing newline.  The function then reads a line from input, converts it
to a string (stripping a trailing newline), and returns that.  When EOF is
read, <a class="reference internal" href="exceptions.html#EOFError" title="EOFError"><tt class="xref py py-exc docutils literal"><span class="pre">EOFError</span></tt></a> is raised.  Example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&#39;--&gt; &#39;</span><span class="p">)</span>  
<span class="go">--&gt; Monty Python&#39;s Flying Circus</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span>  
<span class="go">&quot;Monty Python&#39;s Flying Circus&quot;</span>
</pre></div>
</div>
<p>If the <a class="reference internal" href="readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">readline</span></tt></a> module was loaded, then <a class="reference internal" href="#input" title="input"><tt class="xref py py-func docutils literal"><span class="pre">input()</span></tt></a> will use it
to provide elaborate line editing and history features.</p>
</dd></dl>

<dl class="class">
<dt id="int">
<em class="property">class </em><tt class="descname">int</tt><big>(</big><em>x=0</em><big>)</big><a class="headerlink" href="#int" title="Permalink to this definition">¶</a></dt>
<dt>
<em class="property">class </em><tt class="descname">int</tt><big>(</big><em>x</em>, <em>base=10</em><big>)</big></dt>
<dd><p>Return an integer object constructed from a number or string <em>x</em>, or return
<tt class="docutils literal"><span class="pre">0</span></tt> if no arguments are given.  If <em>x</em> is a number, return
<a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><tt class="xref py py-meth docutils literal"><span class="pre">x.__int__()</span></tt></a>.  For floating point numbers, this
truncates towards zero.</p>
<p>If <em>x</em> is not a number or if <em>base</em> is given, then <em>x</em> must be a string,
<a class="reference internal" href="#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a>, or <a class="reference internal" href="#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> instance representing an <a class="reference internal" href="../reference/lexical_analysis.html#integers"><em>integer
literal</em></a> in radix <em>base</em>.  Optionally, the literal can be
preceded by <tt class="docutils literal"><span class="pre">+</span></tt> or <tt class="docutils literal"><span class="pre">-</span></tt> (with no space in between) and surrounded by
whitespace.  A base-n literal consists of the digits 0 to n-1, with <tt class="docutils literal"><span class="pre">a</span></tt>
to <tt class="docutils literal"><span class="pre">z</span></tt> (or <tt class="docutils literal"><span class="pre">A</span></tt> to <tt class="docutils literal"><span class="pre">Z</span></tt>) having
values 10 to 35.  The default <em>base</em> is 10. The allowed values are 0 and 2-36.
Base-2, -8, and -16 literals can be optionally prefixed with <tt class="docutils literal"><span class="pre">0b</span></tt>/<tt class="docutils literal"><span class="pre">0B</span></tt>,
<tt class="docutils literal"><span class="pre">0o</span></tt>/<tt class="docutils literal"><span class="pre">0O</span></tt>, or <tt class="docutils literal"><span class="pre">0x</span></tt>/<tt class="docutils literal"><span class="pre">0X</span></tt>, as with integer literals in code.  Base 0
means to interpret exactly as a code literal, so that the actual base is 2,
8, 10, or 16, and so that <tt class="docutils literal"><span class="pre">int('010',</span> <span class="pre">0)</span></tt> is not legal, while
<tt class="docutils literal"><span class="pre">int('010')</span></tt> is, as well as <tt class="docutils literal"><span class="pre">int('010',</span> <span class="pre">8)</span></tt>.</p>
<p>The integer type is described in <a class="reference internal" href="stdtypes.html#typesnumeric"><em>Numeric Types &#8212; int, float, complex</em></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>If <em>base</em> is not an instance of <a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> and the <em>base</em> object has a
<a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><tt class="xref py py-meth docutils literal"><span class="pre">base.__index__</span></tt></a> method, that method is called
to obtain an integer for the base.  Previous versions used
<a class="reference internal" href="../reference/datamodel.html#object.__int__" title="object.__int__"><tt class="xref py py-meth docutils literal"><span class="pre">base.__int__</span></tt></a> instead of <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><tt class="xref py py-meth docutils literal"><span class="pre">base.__index__</span></tt></a>.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="isinstance">
<tt class="descname">isinstance</tt><big>(</big><em>object</em>, <em>classinfo</em><big>)</big><a class="headerlink" href="#isinstance" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if the <em>object</em> argument is an instance of the <em>classinfo</em>
argument, or of a (direct, indirect or <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">virtual</em></a>) subclass thereof.  If <em>object</em> is not
an object of the given type, the function always returns false.  If
<em>classinfo</em> is not a class (type object), it may be a tuple of type objects,
or may recursively contain other such tuples (other sequence types are not
accepted).  If <em>classinfo</em> is not a type or tuple of types and such tuples,
a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised.</p>
</dd></dl>

<dl class="function">
<dt id="issubclass">
<tt class="descname">issubclass</tt><big>(</big><em>class</em>, <em>classinfo</em><big>)</big><a class="headerlink" href="#issubclass" title="Permalink to this definition">¶</a></dt>
<dd><p>Return true if <em>class</em> is a subclass (direct, indirect or <a class="reference internal" href="../glossary.html#term-abstract-base-class"><em class="xref std std-term">virtual</em></a>) of <em>classinfo</em>.  A
class is considered a subclass of itself. <em>classinfo</em> may be a tuple of class
objects, in which case every entry in <em>classinfo</em> will be checked. In any other
case, a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception is raised.</p>
</dd></dl>

<dl class="function">
<dt id="iter">
<tt class="descname">iter</tt><big>(</big><em>object</em><span class="optional">[</span>, <em>sentinel</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#iter" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a> object.  The first argument is interpreted very
differently depending on the presence of the second argument. Without a
second argument, <em>object</em> must be a collection object which supports the
iteration protocol (the <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a> method), or it must support the
sequence protocol (the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> method with integer arguments
starting at <tt class="docutils literal"><span class="pre">0</span></tt>).  If it does not support either of those protocols,
<a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> is raised. If the second argument, <em>sentinel</em>, is given,
then <em>object</em> must be a callable object.  The iterator created in this case
will call <em>object</em> with no arguments for each call to its
<a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><tt class="xref py py-meth docutils literal"><span class="pre">__next__()</span></tt></a> method; if the value returned is equal to
<em>sentinel</em>, <a class="reference internal" href="exceptions.html#StopIteration" title="StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> will be raised, otherwise the value will
be returned.</p>
<p>See also <a class="reference internal" href="stdtypes.html#typeiter"><em>Iterator Types</em></a>.</p>
<p>One useful application of the second form of <a class="reference internal" href="#iter" title="iter"><tt class="xref py py-func docutils literal"><span class="pre">iter()</span></tt></a> is to read lines of
a file until a certain line is reached.  The following example reads a file
until the <a class="reference internal" href="io.html#io.TextIOBase.readline" title="io.TextIOBase.readline"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method returns an empty string:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;mydata.txt&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">fp</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">iter</span><span class="p">(</span><span class="n">fp</span><span class="o">.</span><span class="n">readline</span><span class="p">,</span> <span class="s">&#39;&#39;</span><span class="p">):</span>
        <span class="n">process_line</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="len">
<tt class="descname">len</tt><big>(</big><em>s</em><big>)</big><a class="headerlink" href="#len" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the length (the number of items) of an object.  The argument may be a
sequence (such as a string, bytes, tuple, list, or range) or a collection
(such as a dictionary, set, or frozen set).</p>
</dd></dl>

<span class="target" id="func-list"></span><dl class="class">
<dt>
<em class="property">class </em><tt class="descname">list</tt><big>(</big><span class="optional">[</span><em>iterable</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Rather than being a function, <a class="reference internal" href="stdtypes.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a> is actually a mutable
sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq-list"><em>Lists</em></a> and <a class="reference internal" href="stdtypes.html#typesseq"><em>Sequence Types &#8212; list, tuple, range</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="locals">
<tt class="descname">locals</tt><big>(</big><big>)</big><a class="headerlink" href="#locals" title="Permalink to this definition">¶</a></dt>
<dd><p>Update and return a dictionary representing the current local symbol table.
Free variables are returned by <a class="reference internal" href="#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a> when it is called in function
blocks, but not in class blocks.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The contents of this dictionary should not be modified; changes may not
affect the values of local and free variables used by the interpreter.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="map">
<tt class="descname">map</tt><big>(</big><em>function</em>, <em>iterable</em>, <em>...</em><big>)</big><a class="headerlink" href="#map" title="Permalink to this definition">¶</a></dt>
<dd><p>Return an iterator that applies <em>function</em> to every item of <em>iterable</em>,
yielding the results.  If additional <em>iterable</em> arguments are passed,
<em>function</em> must take that many arguments and is applied to the items from all
iterables in parallel.  With multiple iterables, the iterator stops when the
shortest iterable is exhausted.  For cases where the function inputs are
already arranged into argument tuples, see <a class="reference internal" href="itertools.html#itertools.starmap" title="itertools.starmap"><tt class="xref py py-func docutils literal"><span class="pre">itertools.starmap()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="max">
<tt class="descname">max</tt><big>(</big><em>iterable</em>, <em>*</em><span class="optional">[</span>, <em>key</em>, <em>default</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#max" title="Permalink to this definition">¶</a></dt>
<dt>
<tt class="descname">max</tt><big>(</big><em>arg1</em>, <em>arg2</em>, <em>*args</em><span class="optional">[</span>, <em>key</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Return the largest item in an iterable or the largest of two or more
arguments.</p>
<p>If one positional argument is provided, it should be an <a class="reference internal" href="../glossary.html#term-iterable"><em class="xref std std-term">iterable</em></a>.
The largest item in the iterable is returned.  If two or more positional
arguments are provided, the largest of the positional arguments is
returned.</p>
<p>There are two optional keyword-only arguments. The <em>key</em> argument specifies
a one-argument ordering function like that used for <a class="reference internal" href="stdtypes.html#list.sort" title="list.sort"><tt class="xref py py-meth docutils literal"><span class="pre">list.sort()</span></tt></a>. The
<em>default</em> argument specifies an object to return if the provided iterable is
empty. If the iterable is empty and <em>default</em> is not provided, a
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p>
<p>If multiple items are maximal, the function returns the first one
encountered.  This is consistent with other sort-stability preserving tools
such as <tt class="docutils literal"><span class="pre">sorted(iterable,</span> <span class="pre">key=keyfunc,</span> <span class="pre">reverse=True)[0]</span></tt> and
<tt class="docutils literal"><span class="pre">heapq.nlargest(1,</span> <span class="pre">iterable,</span> <span class="pre">key=keyfunc)</span></tt>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4: </span>The <em>default</em> keyword-only argument.</p>
</div>
</dd></dl>

<span class="target" id="func-memoryview"></span><dl class="function">
<dt>
<tt class="descname">memoryview</tt><big>(</big><em>obj</em><big>)</big></dt>
<dd><p>Return a &#8220;memory view&#8221; object created from the given argument.  See
<a class="reference internal" href="stdtypes.html#typememoryview"><em>Memory Views</em></a> for more information.</p>
</dd></dl>

<dl class="function">
<dt id="min">
<tt class="descname">min</tt><big>(</big><em>iterable</em>, <em>*</em><span class="optional">[</span>, <em>key</em>, <em>default</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#min" title="Permalink to this definition">¶</a></dt>
<dt>
<tt class="descname">min</tt><big>(</big><em>arg1</em>, <em>arg2</em>, <em>*args</em><span class="optional">[</span>, <em>key</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Return the smallest item in an iterable or the smallest of two or more
arguments.</p>
<p>If one positional argument is provided, it should be an <a class="reference internal" href="../glossary.html#term-iterable"><em class="xref std std-term">iterable</em></a>.
The smallest item in the iterable is returned.  If two or more positional
arguments are provided, the smallest of the positional arguments is
returned.</p>
<p>There are two optional keyword-only arguments. The <em>key</em> argument specifies
a one-argument ordering function like that used for <a class="reference internal" href="stdtypes.html#list.sort" title="list.sort"><tt class="xref py py-meth docutils literal"><span class="pre">list.sort()</span></tt></a>. The
<em>default</em> argument specifies an object to return if the provided iterable is
empty. If the iterable is empty and <em>default</em> is not provided, a
<a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> is raised.</p>
<p>If multiple items are minimal, the function returns the first one
encountered.  This is consistent with other sort-stability preserving tools
such as <tt class="docutils literal"><span class="pre">sorted(iterable,</span> <span class="pre">key=keyfunc)[0]</span></tt> and <tt class="docutils literal"><span class="pre">heapq.nsmallest(1,</span>
<span class="pre">iterable,</span> <span class="pre">key=keyfunc)</span></tt>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.4: </span>The <em>default</em> keyword-only argument.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="next">
<tt class="descname">next</tt><big>(</big><em>iterator</em><span class="optional">[</span>, <em>default</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#next" title="Permalink to this definition">¶</a></dt>
<dd><p>Retrieve the next item from the <em>iterator</em> by calling its
<a class="reference internal" href="stdtypes.html#iterator.__next__" title="iterator.__next__"><tt class="xref py py-meth docutils literal"><span class="pre">__next__()</span></tt></a> method.  If <em>default</em> is given, it is returned
if the iterator is exhausted, otherwise <a class="reference internal" href="exceptions.html#StopIteration" title="StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> is raised.</p>
</dd></dl>

<dl class="class">
<dt id="object">
<em class="property">class </em><tt class="descname">object</tt><a class="headerlink" href="#object" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new featureless object.  <a class="reference internal" href="#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> is a base for all classes.
It has the methods that are common to all instances of Python classes.  This
function does not accept any arguments.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last"><a class="reference internal" href="#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> does <em>not</em> have a <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt></a>, so you can&#8217;t
assign arbitrary attributes to an instance of the <a class="reference internal" href="#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> class.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="oct">
<tt class="descname">oct</tt><big>(</big><em>x</em><big>)</big><a class="headerlink" href="#oct" title="Permalink to this definition">¶</a></dt>
<dd><p>Convert an integer number to an octal string.  The result is a valid Python
expression.  If <em>x</em> is not a Python <a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a> object, it has to define an
<a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><tt class="xref py py-meth docutils literal"><span class="pre">__index__()</span></tt></a> method that returns an integer.</p>
<span class="target" id="index-5"></span></dd></dl>

<dl class="function">
<dt id="open">
<tt class="descname">open</tt><big>(</big><em>file</em>, <em>mode='r'</em>, <em>buffering=-1</em>, <em>encoding=None</em>, <em>errors=None</em>, <em>newline=None</em>, <em>closefd=True</em>, <em>opener=None</em><big>)</big><a class="headerlink" href="#open" title="Permalink to this definition">¶</a></dt>
<dd><p>Open <em>file</em> and return a corresponding <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a>.  If the file
cannot be opened, an <a class="reference internal" href="exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a> is raised.</p>
<p><em>file</em> is either a string or bytes object giving the pathname (absolute or
relative to the current working directory) of the file to be opened or
an integer file descriptor of the file to be wrapped.  (If a file descriptor
is given, it is closed when the returned I/O object is closed, unless
<em>closefd</em> is set to <tt class="docutils literal"><span class="pre">False</span></tt>.)</p>
<p><em>mode</em> is an optional string that specifies the mode in which the file is
opened.  It defaults to <tt class="docutils literal"><span class="pre">'r'</span></tt> which means open for reading in text mode.
Other common values are <tt class="docutils literal"><span class="pre">'w'</span></tt> for writing (truncating the file if it
already exists), <tt class="docutils literal"><span class="pre">'x'</span></tt> for exclusive creation and <tt class="docutils literal"><span class="pre">'a'</span></tt> for appending
(which on <em>some</em> Unix systems, means that <em>all</em> writes append to the end of
the file regardless of the current seek position).  In text mode, if
<em>encoding</em> is not specified the encoding used is platform dependent:
<tt class="docutils literal"><span class="pre">locale.getpreferredencoding(False)</span></tt> is called to get the current locale
encoding. (For reading and writing raw bytes use binary mode and leave
<em>encoding</em> unspecified.)  The available modes are:</p>
<table border="1" class="docutils">
<colgroup>
<col width="13%" />
<col width="88%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Character</th>
<th class="head">Meaning</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">'r'</span></tt></td>
<td>open for reading (default)</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'w'</span></tt></td>
<td>open for writing, truncating the file first</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">'x'</span></tt></td>
<td>open for exclusive creation, failing if the file already exists</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'a'</span></tt></td>
<td>open for writing, appending to the end of the file if it exists</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">'b'</span></tt></td>
<td>binary mode</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'t'</span></tt></td>
<td>text mode (default)</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">'+'</span></tt></td>
<td>open a disk file for updating (reading and writing)</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">'U'</span></tt></td>
<td><a class="reference internal" href="../glossary.html#term-universal-newlines"><em class="xref std std-term">universal newlines</em></a> mode (deprecated)</td>
</tr>
</tbody>
</table>
<p>The default mode is <tt class="docutils literal"><span class="pre">'r'</span></tt> (open for reading text, synonym of <tt class="docutils literal"><span class="pre">'rt'</span></tt>).
For binary read-write access, the mode <tt class="docutils literal"><span class="pre">'w+b'</span></tt> opens and truncates the file
to 0 bytes.  <tt class="docutils literal"><span class="pre">'r+b'</span></tt> opens the file without truncation.</p>
<p>As mentioned in the <a class="reference internal" href="io.html#io-overview"><em>Overview</em></a>, Python distinguishes between binary
and text I/O.  Files opened in binary mode (including <tt class="docutils literal"><span class="pre">'b'</span></tt> in the <em>mode</em>
argument) return contents as <a class="reference internal" href="#bytes" title="bytes"><tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt></a> objects without any decoding.  In
text mode (the default, or when <tt class="docutils literal"><span class="pre">'t'</span></tt> is included in the <em>mode</em> argument),
the contents of the file are returned as <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a>, the bytes having been
first decoded using a platform-dependent encoding or using the specified
<em>encoding</em> if given.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Python doesn&#8217;t depend on the underlying operating system&#8217;s notion of text
files; all the processing is done by Python itself, and is therefore
platform-independent.</p>
</div>
<p><em>buffering</em> is an optional integer used to set the buffering policy.  Pass 0
to switch buffering off (only allowed in binary mode), 1 to select line
buffering (only usable in text mode), and an integer &gt; 1 to indicate the size
in bytes of a fixed-size chunk buffer.  When no <em>buffering</em> argument is
given, the default buffering policy works as follows:</p>
<ul class="simple">
<li>Binary files are buffered in fixed-size chunks; the size of the buffer is
chosen using a heuristic trying to determine the underlying device&#8217;s &#8220;block
size&#8221; and falling back on <a class="reference internal" href="io.html#io.DEFAULT_BUFFER_SIZE" title="io.DEFAULT_BUFFER_SIZE"><tt class="xref py py-attr docutils literal"><span class="pre">io.DEFAULT_BUFFER_SIZE</span></tt></a>.  On many systems,
the buffer will typically be 4096 or 8192 bytes long.</li>
<li>&#8220;Interactive&#8221; text files (files for which <a class="reference internal" href="io.html#io.IOBase.isatty" title="io.IOBase.isatty"><tt class="xref py py-meth docutils literal"><span class="pre">isatty()</span></tt></a>
returns <tt class="docutils literal"><span class="pre">True</span></tt>) use line buffering.  Other text files use the policy
described above for binary files.</li>
</ul>
<p><em>encoding</em> is the name of the encoding used to decode or encode the file.
This should only be used in text mode.  The default encoding is platform
dependent (whatever <a class="reference internal" href="locale.html#locale.getpreferredencoding" title="locale.getpreferredencoding"><tt class="xref py py-func docutils literal"><span class="pre">locale.getpreferredencoding()</span></tt></a> returns), but any
<a class="reference internal" href="../glossary.html#term-text-encoding"><em class="xref std std-term">text encoding</em></a> supported by Python
can be used.  See the <a class="reference internal" href="codecs.html#module-codecs" title="codecs: Encode and decode data and streams."><tt class="xref py py-mod docutils literal"><span class="pre">codecs</span></tt></a> module for
the list of supported encodings.</p>
<p><em>errors</em> is an optional string that specifies how encoding and decoding
errors are to be handled&#8211;this cannot be used in binary mode.
A variety of standard error handlers are available
(listed under <a class="reference internal" href="codecs.html#error-handlers"><em>Error Handlers</em></a>), though any
error handling name that has been registered with
<a class="reference internal" href="codecs.html#codecs.register_error" title="codecs.register_error"><tt class="xref py py-func docutils literal"><span class="pre">codecs.register_error()</span></tt></a> is also valid.  The standard names
include:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">'strict'</span></tt> to raise a <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> exception if there is
an encoding error.  The default value of <tt class="docutils literal"><span class="pre">None</span></tt> has the same
effect.</li>
<li><tt class="docutils literal"><span class="pre">'ignore'</span></tt> ignores errors.  Note that ignoring encoding errors
can lead to data loss.</li>
<li><tt class="docutils literal"><span class="pre">'replace'</span></tt> causes a replacement marker (such as <tt class="docutils literal"><span class="pre">'?'</span></tt>) to be inserted
where there is malformed data.</li>
<li><tt class="docutils literal"><span class="pre">'surrogateescape'</span></tt> will represent any incorrect bytes as code
points in the Unicode Private Use Area ranging from U+DC80 to
U+DCFF.  These private code points will then be turned back into
the same bytes when the <tt class="docutils literal"><span class="pre">surrogateescape</span></tt> error handler is used
when writing data.  This is useful for processing files in an
unknown encoding.</li>
<li><tt class="docutils literal"><span class="pre">'xmlcharrefreplace'</span></tt> is only supported when writing to a file.
Characters not supported by the encoding are replaced with the
appropriate XML character reference <tt class="docutils literal"><span class="pre">&amp;#nnn;</span></tt>.</li>
<li><tt class="docutils literal"><span class="pre">'backslashreplace'</span></tt> (also only supported when writing)
replaces unsupported characters with Python&#8217;s backslashed escape
sequences.</li>
</ul>
<p id="index-6"><em>newline</em> controls how <a class="reference internal" href="../glossary.html#term-universal-newlines"><em class="xref std std-term">universal newlines</em></a> mode works (it only
applies to text mode).  It can be <tt class="docutils literal"><span class="pre">None</span></tt>, <tt class="docutils literal"><span class="pre">''</span></tt>, <tt class="docutils literal"><span class="pre">'\n'</span></tt>, <tt class="docutils literal"><span class="pre">'\r'</span></tt>, and
<tt class="docutils literal"><span class="pre">'\r\n'</span></tt>.  It works as follows:</p>
<ul class="simple">
<li>When reading input from the stream, if <em>newline</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, universal
newlines mode is enabled.  Lines in the input can end in <tt class="docutils literal"><span class="pre">'\n'</span></tt>,
<tt class="docutils literal"><span class="pre">'\r'</span></tt>, or <tt class="docutils literal"><span class="pre">'\r\n'</span></tt>, and these are translated into <tt class="docutils literal"><span class="pre">'\n'</span></tt> before
being returned to the caller.  If it is <tt class="docutils literal"><span class="pre">''</span></tt>, universal newlines mode is
enabled, but line endings are returned to the caller untranslated.  If it
has any of the other legal values, input lines are only terminated by the
given string, and the line ending is returned to the caller untranslated.</li>
<li>When writing output to the stream, if <em>newline</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, any <tt class="docutils literal"><span class="pre">'\n'</span></tt>
characters written are translated to the system default line separator,
<a class="reference internal" href="os.html#os.linesep" title="os.linesep"><tt class="xref py py-data docutils literal"><span class="pre">os.linesep</span></tt></a>.  If <em>newline</em> is <tt class="docutils literal"><span class="pre">''</span></tt> or <tt class="docutils literal"><span class="pre">'\n'</span></tt>, no translation
takes place.  If <em>newline</em> is any of the other legal values, any <tt class="docutils literal"><span class="pre">'\n'</span></tt>
characters written are translated to the given string.</li>
</ul>
<p>If <em>closefd</em> is <tt class="docutils literal"><span class="pre">False</span></tt> and a file descriptor rather than a filename was
given, the underlying file descriptor will be kept open when the file is
closed.  If a filename is given <em>closefd</em> has no effect and must be <tt class="docutils literal"><span class="pre">True</span></tt>
(the default).</p>
<p>A custom opener can be used by passing a callable as <em>opener</em>. The underlying
file descriptor for the file object is then obtained by calling <em>opener</em> with
(<em>file</em>, <em>flags</em>). <em>opener</em> must return an open file descriptor (passing
<a class="reference internal" href="os.html#os.open" title="os.open"><tt class="xref py py-mod docutils literal"><span class="pre">os.open</span></tt></a> as <em>opener</em> results in functionality similar to passing
<tt class="docutils literal"><span class="pre">None</span></tt>).</p>
<p>The newly created file is <a class="reference internal" href="os.html#fd-inheritance"><em>non-inheritable</em></a>.</p>
<p>The following example uses the <a class="reference internal" href="os.html#dir-fd"><em>dir_fd</em></a> parameter of the
<a class="reference internal" href="os.html#os.open" title="os.open"><tt class="xref py py-func docutils literal"><span class="pre">os.open()</span></tt></a> function to open a file relative to a given directory:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">os</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dir_fd</span> <span class="o">=</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&#39;somedir&#39;</span><span class="p">,</span> <span class="n">os</span><span class="o">.</span><span class="n">O_RDONLY</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">opener</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">flags</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">return</span> <span class="n">os</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">flags</span><span class="p">,</span> <span class="n">dir_fd</span><span class="o">=</span><span class="n">dir_fd</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;spamspam.txt&#39;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">,</span> <span class="n">opener</span><span class="o">=</span><span class="n">opener</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="s">&#39;This will be written to somedir/spamspam.txt&#39;</span><span class="p">,</span> <span class="n">file</span><span class="o">=</span><span class="n">f</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">os</span><span class="o">.</span><span class="n">close</span><span class="p">(</span><span class="n">dir_fd</span><span class="p">)</span>  <span class="c"># don&#39;t leak a file descriptor</span>
</pre></div>
</div>
<p>The type of <a class="reference internal" href="../glossary.html#term-file-object"><em class="xref std std-term">file object</em></a> returned by the <a class="reference internal" href="#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> function
depends on the mode.  When <a class="reference internal" href="#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> is used to open a file in a text
mode (<tt class="docutils literal"><span class="pre">'w'</span></tt>, <tt class="docutils literal"><span class="pre">'r'</span></tt>, <tt class="docutils literal"><span class="pre">'wt'</span></tt>, <tt class="docutils literal"><span class="pre">'rt'</span></tt>, etc.), it returns a subclass of
<a class="reference internal" href="io.html#io.TextIOBase" title="io.TextIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.TextIOBase</span></tt></a> (specifically <a class="reference internal" href="io.html#io.TextIOWrapper" title="io.TextIOWrapper"><tt class="xref py py-class docutils literal"><span class="pre">io.TextIOWrapper</span></tt></a>).  When used
to open a file in a binary mode with buffering, the returned class is a
subclass of <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedIOBase</span></tt></a>.  The exact class varies: in read
binary mode, it returns a <a class="reference internal" href="io.html#io.BufferedReader" title="io.BufferedReader"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedReader</span></tt></a>; in write binary and
append binary modes, it returns a <a class="reference internal" href="io.html#io.BufferedWriter" title="io.BufferedWriter"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedWriter</span></tt></a>, and in
read/write mode, it returns a <a class="reference internal" href="io.html#io.BufferedRandom" title="io.BufferedRandom"><tt class="xref py py-class docutils literal"><span class="pre">io.BufferedRandom</span></tt></a>.  When buffering is
disabled, the raw stream, a subclass of <a class="reference internal" href="io.html#io.RawIOBase" title="io.RawIOBase"><tt class="xref py py-class docutils literal"><span class="pre">io.RawIOBase</span></tt></a>,
<a class="reference internal" href="io.html#io.FileIO" title="io.FileIO"><tt class="xref py py-class docutils literal"><span class="pre">io.FileIO</span></tt></a>, is returned.</p>
<p id="index-7">See also the file handling modules, such as, <a class="reference internal" href="fileinput.html#module-fileinput" title="fileinput: Loop over standard input or a list of files."><tt class="xref py py-mod docutils literal"><span class="pre">fileinput</span></tt></a>, <a class="reference internal" href="io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a>
(where <a class="reference internal" href="#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a> is declared), <a class="reference internal" href="os.html#module-os" title="os: Miscellaneous operating system interfaces."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a>, <a class="reference internal" href="os.path.html#module-os.path" title="os.path: Operations on pathnames."><tt class="xref py py-mod docutils literal"><span class="pre">os.path</span></tt></a>, <a class="reference internal" href="tempfile.html#module-tempfile" title="tempfile: Generate temporary files and directories."><tt class="xref py py-mod docutils literal"><span class="pre">tempfile</span></tt></a>,
and <a class="reference internal" href="shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><tt class="xref py py-mod docutils literal"><span class="pre">shutil</span></tt></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>The <em>opener</em> parameter was added.
The <tt class="docutils literal"><span class="pre">'x'</span></tt> mode was added.
<a class="reference internal" href="exceptions.html#IOError" title="IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> used to be raised, it is now an alias of <a class="reference internal" href="exceptions.html#OSError" title="OSError"><tt class="xref py py-exc docutils literal"><span class="pre">OSError</span></tt></a>.
<a class="reference internal" href="exceptions.html#FileExistsError" title="FileExistsError"><tt class="xref py py-exc docutils literal"><span class="pre">FileExistsError</span></tt></a> is now raised if the file opened in exclusive
creation mode (<tt class="docutils literal"><span class="pre">'x'</span></tt>) already exists.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>The file is now non-inheritable.</p>
</div>
<div class="deprecated-removed">
<p><span class="versionmodified">Deprecated since version 3.4, will be removed in version 4.0: </span>The <tt class="docutils literal"><span class="pre">'U'</span></tt> mode.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="ord">
<tt class="descname">ord</tt><big>(</big><em>c</em><big>)</big><a class="headerlink" href="#ord" title="Permalink to this definition">¶</a></dt>
<dd><p>Given a string representing one Unicode character, return an integer
representing the Unicode code
point of that character.  For example, <tt class="docutils literal"><span class="pre">ord('a')</span></tt> returns the integer <tt class="docutils literal"><span class="pre">97</span></tt>
and <tt class="docutils literal"><span class="pre">ord('\u2020')</span></tt> returns <tt class="docutils literal"><span class="pre">8224</span></tt>.  This is the inverse of <a class="reference internal" href="#chr" title="chr"><tt class="xref py py-func docutils literal"><span class="pre">chr()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="pow">
<tt class="descname">pow</tt><big>(</big><em>x</em>, <em>y</em><span class="optional">[</span>, <em>z</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#pow" title="Permalink to this definition">¶</a></dt>
<dd><p>Return <em>x</em> to the power <em>y</em>; if <em>z</em> is present, return <em>x</em> to the power <em>y</em>,
modulo <em>z</em> (computed more efficiently than <tt class="docutils literal"><span class="pre">pow(x,</span> <span class="pre">y)</span> <span class="pre">%</span> <span class="pre">z</span></tt>). The two-argument
form <tt class="docutils literal"><span class="pre">pow(x,</span> <span class="pre">y)</span></tt> is equivalent to using the power operator: <tt class="docutils literal"><span class="pre">x**y</span></tt>.</p>
<p>The arguments must have numeric types.  With mixed operand types, the
coercion rules for binary arithmetic operators apply.  For <a class="reference internal" href="#int" title="int"><tt class="xref py py-class docutils literal"><span class="pre">int</span></tt></a>
operands, the result has the same type as the operands (after coercion)
unless the second argument is negative; in that case, all arguments are
converted to float and a float result is delivered.  For example, <tt class="docutils literal"><span class="pre">10**2</span></tt>
returns <tt class="docutils literal"><span class="pre">100</span></tt>, but <tt class="docutils literal"><span class="pre">10**-2</span></tt> returns <tt class="docutils literal"><span class="pre">0.01</span></tt>.  If the second argument is
negative, the third argument must be omitted.  If <em>z</em> is present, <em>x</em> and <em>y</em>
must be of integer types, and <em>y</em> must be non-negative.</p>
</dd></dl>

<dl class="function">
<dt id="print">
<tt class="descname">print</tt><big>(</big><em>*objects</em>, <em>sep=' '</em>, <em>end='\n'</em>, <em>file=sys.stdout</em>, <em>flush=False</em><big>)</big><a class="headerlink" href="#print" title="Permalink to this definition">¶</a></dt>
<dd><p>Print <em>objects</em> to the text stream <em>file</em>, separated by <em>sep</em> and followed
by <em>end</em>.  <em>sep</em>, <em>end</em> and <em>file</em>, if present, must be given as keyword
arguments.</p>
<p>All non-keyword arguments are converted to strings like <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> does and
written to the stream, separated by <em>sep</em> and followed by <em>end</em>.  Both <em>sep</em>
and <em>end</em> must be strings; they can also be <tt class="docutils literal"><span class="pre">None</span></tt>, which means to use the
default values.  If no <em>objects</em> are given, <a class="reference internal" href="#print" title="print"><tt class="xref py py-func docutils literal"><span class="pre">print()</span></tt></a> will just write
<em>end</em>.</p>
<p>The <em>file</em> argument must be an object with a <tt class="docutils literal"><span class="pre">write(string)</span></tt> method; if it
is not present or <tt class="docutils literal"><span class="pre">None</span></tt>, <a class="reference internal" href="sys.html#sys.stdout" title="sys.stdout"><tt class="xref py py-data docutils literal"><span class="pre">sys.stdout</span></tt></a> will be used.  Since printed
arguments are converted to text strings, <a class="reference internal" href="#print" title="print"><tt class="xref py py-func docutils literal"><span class="pre">print()</span></tt></a> cannot be used with
binary mode file objects.  For these, use <tt class="docutils literal"><span class="pre">file.write(...)</span></tt> instead.</p>
<p>Whether output is buffered is usually determined by <em>file</em>, but if the
<em>flush</em> keyword argument is true, the stream is forcibly flushed.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>Added the <em>flush</em> keyword argument.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="property">
<em class="property">class </em><tt class="descname">property</tt><big>(</big><em>fget=None</em>, <em>fset=None</em>, <em>fdel=None</em>, <em>doc=None</em><big>)</big><a class="headerlink" href="#property" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a property attribute.</p>
<p><em>fget</em> is a function for getting an attribute value.  <em>fset</em> is a function
for setting an attribute value. <em>fdel</em> is a function for deleting an attribute
value.  And <em>doc</em> creates a docstring for the attribute.</p>
<p>A typical use is to define a managed attribute <tt class="docutils literal"><span class="pre">x</span></tt>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="k">None</span>

    <span class="k">def</span> <span class="nf">getx</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>

    <span class="k">def</span> <span class="nf">setx</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span>

    <span class="k">def</span> <span class="nf">delx</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>

    <span class="n">x</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">getx</span><span class="p">,</span> <span class="n">setx</span><span class="p">,</span> <span class="n">delx</span><span class="p">,</span> <span class="s">&quot;I&#39;m the &#39;x&#39; property.&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>If <em>c</em> is an instance of <em>C</em>, <tt class="docutils literal"><span class="pre">c.x</span></tt> will invoke the getter,
<tt class="docutils literal"><span class="pre">c.x</span> <span class="pre">=</span> <span class="pre">value</span></tt> will invoke the setter and <tt class="docutils literal"><span class="pre">del</span> <span class="pre">c.x</span></tt> the deleter.</p>
<p>If given, <em>doc</em> will be the docstring of the property attribute. Otherwise, the
property will copy <em>fget</em>&#8216;s docstring (if it exists).  This makes it possible to
create read-only properties easily using <a class="reference internal" href="#property" title="property"><tt class="xref py py-func docutils literal"><span class="pre">property()</span></tt></a> as a <a class="reference internal" href="../glossary.html#term-decorator"><em class="xref std std-term">decorator</em></a>:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Parrot</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_voltage</span> <span class="o">=</span> <span class="mi">100000</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">voltage</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Get the current voltage.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_voltage</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">&#64;property</span></tt> decorator turns the <tt class="xref py py-meth docutils literal"><span class="pre">voltage()</span></tt> method into a &#8220;getter&#8221;
for a read-only attribute with the same name, and it sets the docstring for
<em>voltage</em> to &#8220;Get the current voltage.&#8221;</p>
<p>A property object has <tt class="xref py py-attr docutils literal"><span class="pre">getter</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">setter</span></tt>,
and <tt class="xref py py-attr docutils literal"><span class="pre">deleter</span></tt> methods usable as decorators that create a
copy of the property with the corresponding accessor function set to the
decorated function.  This is best explained with an example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="k">None</span>

    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;I&#39;m the &#39;x&#39; property.&quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>

    <span class="nd">@x</span><span class="o">.</span><span class="n">setter</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span>

    <span class="nd">@x</span><span class="o">.</span><span class="n">deleter</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>
</pre></div>
</div>
<p>This code is exactly equivalent to the first example.  Be sure to give the
additional functions the same name as the original property (<tt class="docutils literal"><span class="pre">x</span></tt> in this
case.)</p>
<p>The returned property object also has the attributes <tt class="docutils literal"><span class="pre">fget</span></tt>, <tt class="docutils literal"><span class="pre">fset</span></tt>, and
<tt class="docutils literal"><span class="pre">fdel</span></tt> corresponding to the constructor arguments.</p>
</dd></dl>

<span class="target" id="func-range"></span><dl class="function">
<dt>
<tt class="descname">range</tt><big>(</big><em>stop</em><big>)</big></dt>
<dt>
<tt class="descname">range</tt><big>(</big><em>start</em>, <em>stop</em><span class="optional">[</span>, <em>step</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Rather than being a function, <a class="reference internal" href="stdtypes.html#range" title="range"><tt class="xref py py-class docutils literal"><span class="pre">range</span></tt></a> is actually an immutable
sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq-range"><em>Ranges</em></a> and <a class="reference internal" href="stdtypes.html#typesseq"><em>Sequence Types &#8212; list, tuple, range</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="repr">
<tt class="descname">repr</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#repr" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a string containing a printable representation of an object.  For many
types, this function makes an attempt to return a string that would yield an
object with the same value when passed to <a class="reference internal" href="#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a>, otherwise the
representation is a string enclosed in angle brackets that contains the name
of the type of the object together with additional information often
including the name and address of the object.  A class can control what this
function returns for its instances by defining a <a class="reference internal" href="../reference/datamodel.html#object.__repr__" title="object.__repr__"><tt class="xref py py-meth docutils literal"><span class="pre">__repr__()</span></tt></a> method.</p>
</dd></dl>

<dl class="function">
<dt id="reversed">
<tt class="descname">reversed</tt><big>(</big><em>seq</em><big>)</big><a class="headerlink" href="#reversed" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a reverse <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a>.  <em>seq</em> must be an object which has
a <a class="reference internal" href="../reference/datamodel.html#object.__reversed__" title="object.__reversed__"><tt class="xref py py-meth docutils literal"><span class="pre">__reversed__()</span></tt></a> method or supports the sequence protocol (the
<a class="reference internal" href="../reference/datamodel.html#object.__len__" title="object.__len__"><tt class="xref py py-meth docutils literal"><span class="pre">__len__()</span></tt></a> method and the <a class="reference internal" href="../reference/datamodel.html#object.__getitem__" title="object.__getitem__"><tt class="xref py py-meth docutils literal"><span class="pre">__getitem__()</span></tt></a> method with integer
arguments starting at <tt class="docutils literal"><span class="pre">0</span></tt>).</p>
</dd></dl>

<dl class="function">
<dt id="round">
<tt class="descname">round</tt><big>(</big><em>number</em><span class="optional">[</span>, <em>ndigits</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#round" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the floating point value <em>number</em> rounded to <em>ndigits</em> digits after
the decimal point.  If <em>ndigits</em> is omitted, it defaults to zero. Delegates
to <tt class="docutils literal"><span class="pre">number.__round__(ndigits)</span></tt>.</p>
<p>For the built-in types supporting <a class="reference internal" href="#round" title="round"><tt class="xref py py-func docutils literal"><span class="pre">round()</span></tt></a>, values are rounded to the
closest multiple of 10 to the power minus <em>ndigits</em>; if two multiples are
equally close, rounding is done toward the even choice (so, for example,
both <tt class="docutils literal"><span class="pre">round(0.5)</span></tt> and <tt class="docutils literal"><span class="pre">round(-0.5)</span></tt> are <tt class="docutils literal"><span class="pre">0</span></tt>, and <tt class="docutils literal"><span class="pre">round(1.5)</span></tt> is
<tt class="docutils literal"><span class="pre">2</span></tt>).  The return value is an integer if called with one argument,
otherwise of the same type as <em>number</em>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The behavior of <a class="reference internal" href="#round" title="round"><tt class="xref py py-func docutils literal"><span class="pre">round()</span></tt></a> for floats can be surprising: for example,
<tt class="docutils literal"><span class="pre">round(2.675,</span> <span class="pre">2)</span></tt> gives <tt class="docutils literal"><span class="pre">2.67</span></tt> instead of the expected <tt class="docutils literal"><span class="pre">2.68</span></tt>.
This is not a bug: it&#8217;s a result of the fact that most decimal fractions
can&#8217;t be represented exactly as a float.  See <a class="reference internal" href="../tutorial/floatingpoint.html#tut-fp-issues"><em>Floating Point Arithmetic:  Issues and Limitations</em></a> for
more information.</p>
</div>
</dd></dl>

<span class="target" id="func-set"></span><dl class="class">
<dt>
<em class="property">class </em><tt class="descname">set</tt><big>(</big><span class="optional">[</span><em>iterable</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Return a new <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> object, optionally with elements taken from
<em>iterable</em>.  <tt class="docutils literal"><span class="pre">set</span></tt> is a built-in class.  See <a class="reference internal" href="stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> and
<a class="reference internal" href="stdtypes.html#types-set"><em>Set Types &#8212; set, frozenset</em></a> for documentation about this class.</p>
<p>For other containers see the built-in <a class="reference internal" href="stdtypes.html#frozenset" title="frozenset"><tt class="xref py py-class docutils literal"><span class="pre">frozenset</span></tt></a>, <a class="reference internal" href="stdtypes.html#list" title="list"><tt class="xref py py-class docutils literal"><span class="pre">list</span></tt></a>,
<a class="reference internal" href="stdtypes.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a>, and <a class="reference internal" href="stdtypes.html#dict" title="dict"><tt class="xref py py-class docutils literal"><span class="pre">dict</span></tt></a> classes, as well as the <a class="reference internal" href="collections.html#module-collections" title="collections: Container datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a>
module.</p>
</dd></dl>

<dl class="function">
<dt id="setattr">
<tt class="descname">setattr</tt><big>(</big><em>object</em>, <em>name</em>, <em>value</em><big>)</big><a class="headerlink" href="#setattr" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the counterpart of <a class="reference internal" href="#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a>.  The arguments are an object, a
string and an arbitrary value.  The string may name an existing attribute or a
new attribute.  The function assigns the value to the attribute, provided the
object allows it.  For example, <tt class="docutils literal"><span class="pre">setattr(x,</span> <span class="pre">'foobar',</span> <span class="pre">123)</span></tt> is equivalent to
<tt class="docutils literal"><span class="pre">x.foobar</span> <span class="pre">=</span> <span class="pre">123</span></tt>.</p>
</dd></dl>

<dl class="class">
<dt id="slice">
<em class="property">class </em><tt class="descname">slice</tt><big>(</big><em>stop</em><big>)</big><a class="headerlink" href="#slice" title="Permalink to this definition">¶</a></dt>
<dt>
<em class="property">class </em><tt class="descname">slice</tt><big>(</big><em>start</em>, <em>stop</em><span class="optional">[</span>, <em>step</em><span class="optional">]</span><big>)</big></dt>
<dd><p id="index-8">Return a <a class="reference internal" href="../glossary.html#term-slice"><em class="xref std std-term">slice</em></a> object representing the set of indices specified by
<tt class="docutils literal"><span class="pre">range(start,</span> <span class="pre">stop,</span> <span class="pre">step)</span></tt>.  The <em>start</em> and <em>step</em> arguments default to
<tt class="docutils literal"><span class="pre">None</span></tt>.  Slice objects have read-only data attributes <tt class="xref py py-attr docutils literal"><span class="pre">start</span></tt>,
<tt class="xref py py-attr docutils literal"><span class="pre">stop</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">step</span></tt> which merely return the argument
values (or their default).  They have no other explicit functionality;
however they are used by Numerical Python and other third party extensions.
Slice objects are also generated when extended indexing syntax is used.  For
example: <tt class="docutils literal"><span class="pre">a[start:stop:step]</span></tt> or <tt class="docutils literal"><span class="pre">a[start:stop,</span> <span class="pre">i]</span></tt>.  See
<a class="reference internal" href="itertools.html#itertools.islice" title="itertools.islice"><tt class="xref py py-func docutils literal"><span class="pre">itertools.islice()</span></tt></a> for an alternate version that returns an iterator.</p>
</dd></dl>

<dl class="function">
<dt id="sorted">
<tt class="descname">sorted</tt><big>(</big><em>iterable[, key][, reverse]</em><big>)</big><a class="headerlink" href="#sorted" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a new sorted list from the items in <em>iterable</em>.</p>
<p>Has two optional arguments which must be specified as keyword arguments.</p>
<p><em>key</em> specifies a function of one argument that is used to extract a comparison
key from each list element: <tt class="docutils literal"><span class="pre">key=str.lower</span></tt>.  The default value is <tt class="docutils literal"><span class="pre">None</span></tt>
(compare the elements directly).</p>
<p><em>reverse</em> is a boolean value.  If set to <tt class="docutils literal"><span class="pre">True</span></tt>, then the list elements are
sorted as if each comparison were reversed.</p>
<p>Use <a class="reference internal" href="functools.html#functools.cmp_to_key" title="functools.cmp_to_key"><tt class="xref py py-func docutils literal"><span class="pre">functools.cmp_to_key()</span></tt></a> to convert an old-style <em>cmp</em> function to a
<em>key</em> function.</p>
<p>The built-in <a class="reference internal" href="#sorted" title="sorted"><tt class="xref py py-func docutils literal"><span class="pre">sorted()</span></tt></a> function is guaranteed to be stable. A sort is
stable if it guarantees not to change the relative order of elements that
compare equal &#8212; this is helpful for sorting in multiple passes (for
example, sort by department, then by salary grade).</p>
<p>For sorting examples and a brief sorting tutorial, see <a class="reference external" href="https://wiki.python.org/moin/HowTo/Sorting/">Sorting HowTo</a>.</p>
</dd></dl>

<dl class="function">
<dt id="staticmethod">
<tt class="descname">staticmethod</tt><big>(</big><em>function</em><big>)</big><a class="headerlink" href="#staticmethod" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a static method for <em>function</em>.</p>
<p>A static method does not receive an implicit first argument. To declare a static
method, use this idiom:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">:</span>
    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="o">...</span><span class="p">):</span> <span class="o">...</span>
</pre></div>
</div>
<p>The <tt class="docutils literal"><span class="pre">&#64;staticmethod</span></tt> form is a function <a class="reference internal" href="../glossary.html#term-decorator"><em class="xref std std-term">decorator</em></a> &#8211; see the
description of function definitions in <a class="reference internal" href="../reference/compound_stmts.html#function"><em>Function definitions</em></a> for details.</p>
<p>It can be called either on the class (such as <tt class="docutils literal"><span class="pre">C.f()</span></tt>) or on an instance (such
as <tt class="docutils literal"><span class="pre">C().f()</span></tt>).  The instance is ignored except for its class.</p>
<p>Static methods in Python are similar to those found in Java or C++. Also see
<a class="reference internal" href="#classmethod" title="classmethod"><tt class="xref py py-func docutils literal"><span class="pre">classmethod()</span></tt></a> for a variant that is useful for creating alternate class
constructors.</p>
<p>For more information on static methods, consult the documentation on the
standard type hierarchy in <a class="reference internal" href="../reference/datamodel.html#types"><em>The standard type hierarchy</em></a>.</p>
</dd></dl>

<span class="target" id="func-str"><span id="index-9"></span></span><dl class="class">
<dt>
<em class="property">class </em><tt class="descname">str</tt><big>(</big><em>object=''</em><big>)</big></dt>
<dt>
<em class="property">class </em><tt class="descname">str</tt><big>(</big><em>object=b''</em>, <em>encoding='utf-8'</em>, <em>errors='strict'</em><big>)</big></dt>
<dd><p>Return a <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> version of <em>object</em>.  See <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> for details.</p>
<p><tt class="docutils literal"><span class="pre">str</span></tt> is the built-in string <a class="reference internal" href="../glossary.html#term-class"><em class="xref std std-term">class</em></a>.  For general information
about strings, see <a class="reference internal" href="stdtypes.html#textseq"><em>Text Sequence Type &#8212; str</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="sum">
<tt class="descname">sum</tt><big>(</big><em>iterable</em><span class="optional">[</span>, <em>start</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#sum" title="Permalink to this definition">¶</a></dt>
<dd><p>Sums <em>start</em> and the items of an <em>iterable</em> from left to right and returns the
total.  <em>start</em> defaults to <tt class="docutils literal"><span class="pre">0</span></tt>. The <em>iterable</em>&#8216;s items are normally numbers,
and the start value is not allowed to be a string.</p>
<p>For some use cases, there are good alternatives to <a class="reference internal" href="#sum" title="sum"><tt class="xref py py-func docutils literal"><span class="pre">sum()</span></tt></a>.
The preferred, fast way to concatenate a sequence of strings is by calling
<tt class="docutils literal"><span class="pre">''.join(sequence)</span></tt>.  To add floating point values with extended precision,
see <a class="reference internal" href="math.html#math.fsum" title="math.fsum"><tt class="xref py py-func docutils literal"><span class="pre">math.fsum()</span></tt></a>.  To concatenate a series of iterables, consider using
<a class="reference internal" href="itertools.html#itertools.chain" title="itertools.chain"><tt class="xref py py-func docutils literal"><span class="pre">itertools.chain()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="super">
<tt class="descname">super</tt><big>(</big><span class="optional">[</span><em>type</em><span class="optional">[</span>, <em>object-or-type</em><span class="optional">]</span><span class="optional">]</span><big>)</big><a class="headerlink" href="#super" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a proxy object that delegates method calls to a parent or sibling
class of <em>type</em>.  This is useful for accessing inherited methods that have
been overridden in a class. The search order is same as that used by
<a class="reference internal" href="#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a> except that the <em>type</em> itself is skipped.</p>
<p>The <a class="reference internal" href="stdtypes.html#class.__mro__" title="class.__mro__"><tt class="xref py py-attr docutils literal"><span class="pre">__mro__</span></tt></a> attribute of the <em>type</em> lists the method
resolution search order used by both <a class="reference internal" href="#getattr" title="getattr"><tt class="xref py py-func docutils literal"><span class="pre">getattr()</span></tt></a> and <a class="reference internal" href="#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a>.  The
attribute is dynamic and can change whenever the inheritance hierarchy is
updated.</p>
<p>If the second argument is omitted, the super object returned is unbound.  If
the second argument is an object, <tt class="docutils literal"><span class="pre">isinstance(obj,</span> <span class="pre">type)</span></tt> must be true.  If
the second argument is a type, <tt class="docutils literal"><span class="pre">issubclass(type2,</span> <span class="pre">type)</span></tt> must be true (this
is useful for classmethods).</p>
<p>There are two typical use cases for <em>super</em>.  In a class hierarchy with
single inheritance, <em>super</em> can be used to refer to parent classes without
naming them explicitly, thus making the code more maintainable.  This use
closely parallels the use of <em>super</em> in other programming languages.</p>
<p>The second use case is to support cooperative multiple inheritance in a
dynamic execution environment.  This use case is unique to Python and is
not found in statically compiled languages or languages that only support
single inheritance.  This makes it possible to implement &#8220;diamond diagrams&#8221;
where multiple base classes implement the same method.  Good design dictates
that this method have the same calling signature in every case (because the
order of calls is determined at runtime, because that order adapts
to changes in the class hierarchy, and because that order can include
sibling classes that are unknown prior to runtime).</p>
<p>For both use cases, a typical superclass call looks like this:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="n">B</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">method</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>    <span class="c"># This does the same thing as:</span>
                               <span class="c"># super(C, self).method(arg)</span>
</pre></div>
</div>
<p>Note that <a class="reference internal" href="#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a> is implemented as part of the binding process for
explicit dotted attribute lookups such as <tt class="docutils literal"><span class="pre">super().__getitem__(name)</span></tt>.
It does so by implementing its own <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattribute__()</span></tt></a> method for searching
classes in a predictable order that supports cooperative multiple inheritance.
Accordingly, <a class="reference internal" href="#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a> is undefined for implicit lookups using statements or
operators such as <tt class="docutils literal"><span class="pre">super()[name]</span></tt>.</p>
<p>Also note that, aside from the zero argument form, <a class="reference internal" href="#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a> is not
limited to use inside methods.  The two argument form specifies the
arguments exactly and makes the appropriate references.  The zero
argument form only works inside a class definition, as the compiler fills
in the necessary details to correctly retrieve the class being defined,
as well as accessing the current instance for ordinary methods.</p>
<p>For practical suggestions on how to design cooperative classes using
<a class="reference internal" href="#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a>, see <a class="reference external" href="http://rhettinger.wordpress.com/2011/05/26/super-considered-super/">guide to using super()</a>.</p>
</dd></dl>

<span class="target" id="func-tuple"></span><dl class="function">
<dt>
<tt class="descname">tuple</tt><big>(</big><span class="optional">[</span><em>iterable</em><span class="optional">]</span><big>)</big></dt>
<dd><p>Rather than being a function, <a class="reference internal" href="stdtypes.html#tuple" title="tuple"><tt class="xref py py-class docutils literal"><span class="pre">tuple</span></tt></a> is actually an immutable
sequence type, as documented in <a class="reference internal" href="stdtypes.html#typesseq-tuple"><em>Tuples</em></a> and <a class="reference internal" href="stdtypes.html#typesseq"><em>Sequence Types &#8212; list, tuple, range</em></a>.</p>
</dd></dl>

<dl class="class">
<dt id="type">
<em class="property">class </em><tt class="descname">type</tt><big>(</big><em>object</em><big>)</big><a class="headerlink" href="#type" title="Permalink to this definition">¶</a></dt>
<dt>
<em class="property">class </em><tt class="descname">type</tt><big>(</big><em>name</em>, <em>bases</em>, <em>dict</em><big>)</big></dt>
<dd><p id="index-10">With one argument, return the type of an <em>object</em>.  The return value is a
type object and generally the same object as returned by
<a class="reference internal" href="stdtypes.html#instance.__class__" title="instance.__class__"><tt class="xref py py-attr docutils literal"><span class="pre">object.__class__</span></tt></a>.</p>
<p>The <a class="reference internal" href="#isinstance" title="isinstance"><tt class="xref py py-func docutils literal"><span class="pre">isinstance()</span></tt></a> built-in function is recommended for testing the type
of an object, because it takes subclasses into account.</p>
<p>With three arguments, return a new type object.  This is essentially a
dynamic form of the <a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> statement. The <em>name</em> string is the
class name and becomes the <a class="reference internal" href="stdtypes.html#class.__name__" title="class.__name__"><tt class="xref py py-attr docutils literal"><span class="pre">__name__</span></tt></a> attribute; the <em>bases</em>
tuple itemizes the base classes and becomes the <a class="reference internal" href="stdtypes.html#class.__bases__" title="class.__bases__"><tt class="xref py py-attr docutils literal"><span class="pre">__bases__</span></tt></a>
attribute; and the <em>dict</em> dictionary is the namespace containing definitions
for class body and becomes the <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt></a> attribute.  For
example, the following two statements create identical <a class="reference internal" href="#type" title="type"><tt class="xref py py-class docutils literal"><span class="pre">type</span></tt></a> objects:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">X</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">a</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">X</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="s">&#39;X&#39;</span><span class="p">,</span> <span class="p">(</span><span class="nb">object</span><span class="p">,),</span> <span class="nb">dict</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
</pre></div>
</div>
<p>See also <a class="reference internal" href="stdtypes.html#bltin-type-objects"><em>Type Objects</em></a>.</p>
</dd></dl>

<dl class="function">
<dt id="vars">
<tt class="descname">vars</tt><big>(</big><span class="optional">[</span><em>object</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#vars" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt></a> attribute for a module, class, instance,
or any other object with a <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt> attribute.</p>
<p>Objects such as modules and instances have an updateable <tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt>
attribute; however, other objects may have write restrictions on their
<tt class="xref py py-attr docutils literal"><span class="pre">__dict__</span></tt> attributes (for example, classes use a
dictproxy to prevent direct dictionary updates).</p>
<p>Without an argument, <a class="reference internal" href="#vars" title="vars"><tt class="xref py py-func docutils literal"><span class="pre">vars()</span></tt></a> acts like <a class="reference internal" href="#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a>.  Note, the
locals dictionary is only useful for reads since updates to the locals
dictionary are ignored.</p>
</dd></dl>

<dl class="function">
<dt id="zip">
<tt class="descname">zip</tt><big>(</big><em>*iterables</em><big>)</big><a class="headerlink" href="#zip" title="Permalink to this definition">¶</a></dt>
<dd><p>Make an iterator that aggregates elements from each of the iterables.</p>
<p>Returns an iterator of tuples, where the <em>i</em>-th tuple contains
the <em>i</em>-th element from each of the argument sequences or iterables.  The
iterator stops when the shortest input iterable is exhausted. With a single
iterable argument, it returns an iterator of 1-tuples.  With no arguments,
it returns an empty iterator.  Equivalent to:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">def</span> <span class="nf">zip</span><span class="p">(</span><span class="o">*</span><span class="n">iterables</span><span class="p">):</span>
    <span class="c"># zip(&#39;ABCD&#39;, &#39;xy&#39;) --&gt; Ax By</span>
    <span class="n">sentinel</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span>
    <span class="n">iterators</span> <span class="o">=</span> <span class="p">[</span><span class="nb">iter</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">iterables</span><span class="p">]</span>
    <span class="k">while</span> <span class="n">iterators</span><span class="p">:</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">iterators</span><span class="p">:</span>
            <span class="n">elem</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">,</span> <span class="n">sentinel</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">elem</span> <span class="ow">is</span> <span class="n">sentinel</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">elem</span><span class="p">)</span>
        <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
</pre></div>
</div>
<p>The left-to-right evaluation order of the iterables is guaranteed. This
makes possible an idiom for clustering a data series into n-length groups
using <tt class="docutils literal"><span class="pre">zip(*[iter(s)]*n)</span></tt>.</p>
<p><a class="reference internal" href="#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a> should only be used with unequal length inputs when you don&#8217;t
care about trailing, unmatched values from the longer iterables.  If those
values are important, use <a class="reference internal" href="itertools.html#itertools.zip_longest" title="itertools.zip_longest"><tt class="xref py py-func docutils literal"><span class="pre">itertools.zip_longest()</span></tt></a> instead.</p>
<p><a class="reference internal" href="#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a> in conjunction with the <tt class="docutils literal"><span class="pre">*</span></tt> operator can be used to unzip a
list:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">y</span> <span class="o">=</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">zipped</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">zipped</span><span class="p">)</span>
<span class="go">[(1, 4), (2, 5), (3, 6)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x2</span><span class="p">,</span> <span class="n">y2</span> <span class="o">=</span> <span class="nb">zip</span><span class="p">(</span><span class="o">*</span><span class="nb">zip</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">x</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">x2</span><span class="p">)</span> <span class="ow">and</span> <span class="n">y</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">y2</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="__import__">
<tt class="descname">__import__</tt><big>(</big><em>name</em>, <em>globals=None</em>, <em>locals=None</em>, <em>fromlist=()</em>, <em>level=0</em><big>)</big><a class="headerlink" href="#__import__" title="Permalink to this definition">¶</a></dt>
<dd><div class="admonition note" id="index-11">
<p class="first admonition-title">Note</p>
<p class="last">This is an advanced function that is not needed in everyday Python
programming, unlike <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><tt class="xref py py-func docutils literal"><span class="pre">importlib.import_module()</span></tt></a>.</p>
</div>
<p>This function is invoked by the <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement.  It can be
replaced (by importing the <a class="reference internal" href="builtins.html#module-builtins" title="builtins: The module that provides the built-in namespace."><tt class="xref py py-mod docutils literal"><span class="pre">builtins</span></tt></a> module and assigning to
<tt class="docutils literal"><span class="pre">builtins.__import__</span></tt>) in order to change semantics of the
<a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement, but doing so is <strong>strongly</strong> discouraged as it
is usually simpler to use import hooks (see <span class="target" id="index-12"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0302"><strong>PEP 302</strong></a>) to attain the same
goals and does not cause issues with code which assumes the default import
implementation is in use.  Direct use of <a class="reference internal" href="#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a> is also
discouraged in favor of <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><tt class="xref py py-func docutils literal"><span class="pre">importlib.import_module()</span></tt></a>.</p>
<p>The function imports the module <em>name</em>, potentially using the given <em>globals</em>
and <em>locals</em> to determine how to interpret the name in a package context.
The <em>fromlist</em> gives the names of objects or submodules that should be
imported from the module given by <em>name</em>.  The standard implementation does
not use its <em>locals</em> argument at all, and uses its <em>globals</em> only to
determine the package context of the <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement.</p>
<p><em>level</em> specifies whether to use absolute or relative imports. <tt class="docutils literal"><span class="pre">0</span></tt> (the
default) means only perform absolute imports.  Positive values for
<em>level</em> indicate the number of parent directories to search relative to the
directory of the module calling <a class="reference internal" href="#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a> (see <span class="target" id="index-13"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0328"><strong>PEP 328</strong></a> for the
details).</p>
<p>When the <em>name</em> variable is of the form <tt class="docutils literal"><span class="pre">package.module</span></tt>, normally, the
top-level package (the name up till the first dot) is returned, <em>not</em> the
module named by <em>name</em>.  However, when a non-empty <em>fromlist</em> argument is
given, the module named by <em>name</em> is returned.</p>
<p>For example, the statement <tt class="docutils literal"><span class="pre">import</span> <span class="pre">spam</span></tt> results in bytecode resembling the
following code:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">spam</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="s">&#39;spam&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="nb">locals</span><span class="p">(),</span> <span class="p">[],</span> <span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>The statement <tt class="docutils literal"><span class="pre">import</span> <span class="pre">spam.ham</span></tt> results in this call:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">spam</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="s">&#39;spam.ham&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="nb">locals</span><span class="p">(),</span> <span class="p">[],</span> <span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>Note how <a class="reference internal" href="#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a> returns the toplevel module here because this is
the object that is bound to a name by the <a class="reference internal" href="../reference/simple_stmts.html#import"><tt class="xref std std-keyword docutils literal"><span class="pre">import</span></tt></a> statement.</p>
<p>On the other hand, the statement <tt class="docutils literal"><span class="pre">from</span> <span class="pre">spam.ham</span> <span class="pre">import</span> <span class="pre">eggs,</span> <span class="pre">sausage</span> <span class="pre">as</span>
<span class="pre">saus</span></tt> results in</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">_temp</span> <span class="o">=</span> <span class="nb">__import__</span><span class="p">(</span><span class="s">&#39;spam.ham&#39;</span><span class="p">,</span> <span class="nb">globals</span><span class="p">(),</span> <span class="nb">locals</span><span class="p">(),</span> <span class="p">[</span><span class="s">&#39;eggs&#39;</span><span class="p">,</span> <span class="s">&#39;sausage&#39;</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>
<span class="n">eggs</span> <span class="o">=</span> <span class="n">_temp</span><span class="o">.</span><span class="n">eggs</span>
<span class="n">saus</span> <span class="o">=</span> <span class="n">_temp</span><span class="o">.</span><span class="n">sausage</span>
</pre></div>
</div>
<p>Here, the <tt class="docutils literal"><span class="pre">spam.ham</span></tt> module is returned from <a class="reference internal" href="#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a>.  From this
object, the names to import are retrieved and assigned to their respective
names.</p>
<p>If you simply want to import a module (potentially within a package) by name,
use <a class="reference internal" href="importlib.html#importlib.import_module" title="importlib.import_module"><tt class="xref py py-func docutils literal"><span class="pre">importlib.import_module()</span></tt></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>Negative values for <em>level</em> are no longer supported (which also changes
the default value to 0).</p>
</div>
</dd></dl>

<p class="rubric">Footnotes</p>
<table class="docutils footnote" frame="void" id="id2" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id1">[1]</a></td><td>Note that the parser only accepts the Unix-style end of line convention.
If you are reading the code from a file, make sure to use newline conversion
mode to convert Windows or Mac-style newlines.</td></tr>
</tbody>
</table>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="intro.html"
                        title="previous chapter">1. Introduction</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="constants.html"
                        title="next chapter">3. Built-in Constants</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/functions.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="constants.html" title="3. Built-in Constants"
             >next</a> |</li>
        <li class="right" >
          <a href="intro.html" title="1. Introduction"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on May 23, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>